diff --git a/README.md b/README.md index 16eddb8..f505fea 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,18 @@ waitUntil {condition}; waitUntil {expression;condition}; ``` +**code** + +The code function is used to compile inline code. This does **not** replace SQF compile buildin function, but will return the contained ASL code as SQF. + +``` +// input: +var x = code("var y = 5;"); // pass as string + +// output: +x = {y = 5;}; +``` + ## List of all keywords Keywords should not be used as identifiers. Here is a full list of all keywords in ASL. Remember that build in function names should not be used neither. diff --git a/in/simple.asl b/in/simple.asl index 268016e..d717c60 100644 --- a/in/simple.asl +++ b/in/simple.asl @@ -1,4 +1 @@ -switch x { - case 1: - x = 1; -} +var inline_code = code("var x = 5;"); diff --git a/src/asl/tokenizer.go b/src/asl/tokenizer.go index f8890a5..3e720c5 100644 --- a/src/asl/tokenizer.go +++ b/src/asl/tokenizer.go @@ -45,7 +45,8 @@ var keywords = []string{ "try", "catch", "exitwith", - "waituntil"} + "waituntil", + "code"} var whitespace = []byte{' ', '\n', '\t', '\r'} var identifier = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" diff --git a/src/asl/tokenizer_test.go b/src/asl/tokenizer_test.go index 4b8c52e..42fae3f 100644 --- a/src/asl/tokenizer_test.go +++ b/src/asl/tokenizer_test.go @@ -77,6 +77,14 @@ func TestTokenizerIdentifier(t *testing.T) { compareTokens(t, &got, &want) } +func TestTokenizerInlineCode(t *testing.T) { + got := getTokens(t, "test/tokenizer_code.asl") + want := []string{"var", "x", "=", "code", "(", "\"var x = 5;\"", ")", ";"} + + compareLength(t, &got, &want) + compareTokens(t, &got, &want) +} + func compareLength(t *testing.T, got *[]Token, want *[]string) { if len(*got) != len(*want) { t.Error("Length of tokens got and expected tokens not equal, was:") diff --git a/test/tokenizer_code.asl b/test/tokenizer_code.asl new file mode 100644 index 0000000..ffbb49d --- /dev/null +++ b/test/tokenizer_code.asl @@ -0,0 +1 @@ +var x = code("var x = 5;");