Added try ... catch.

This commit is contained in:
Marvin Blum
2015-10-25 13:51:53 +01:00
parent 9db5c2042d
commit c3c4ca6f7d
7 changed files with 39 additions and 6 deletions

View File

@@ -10,7 +10,7 @@
* ~~inline buildin function call -> foo(a)(bar(x)(y));~~ * ~~inline buildin function call -> foo(a)(bar(x)(y));~~
* ~~negative values e.g. -1, operator !~~ * ~~negative values e.g. -1, operator !~~
* ~~tokenizer splits commands like "format" -> for, mat~~ * ~~tokenizer splits commands like "format" -> for, mat~~
* try ... catch * ~~try ... catch~~
## Special cases ## Special cases

View File

@@ -1,3 +1,6 @@
// TODO: try {
//var _x = setHitIndex(vehicle()(player))(1, 1); var x = 100;
doSomething(x);
} catch {
errorHandling();
}

View File

@@ -35,6 +35,8 @@ func parseBlock() {
parseFunction() parseFunction()
} else if accept("return") { } else if accept("return") {
parseReturn() parseReturn()
} else if accept("try") {
parseTryCatch()
} else if accept("case") || accept("default") { } else if accept("case") || accept("default") {
return return
} else { } else {
@@ -232,6 +234,20 @@ func parseReturn() {
appendOut(";", true) appendOut(";", true)
} }
func parseTryCatch() {
expect("try")
expect("{")
appendOut("try {", true)
parseBlock()
expect("}")
expect("catch")
expect("{")
appendOut("} catch {", true)
parseBlock()
expect("}")
appendOut("};", true)
}
// Everything that does not start with a keyword. // Everything that does not start with a keyword.
func parseStatement() { func parseStatement() {
// empty block // empty block

View File

@@ -101,6 +101,13 @@ func TestParserOperator(t *testing.T) {
equal(t, got, want) equal(t, got, want)
} }
func TestParserTryCatch(t *testing.T) {
got := getCompiled(t, "test/parser_try_catch.asl")
want := "try {\n} catch {\n};\n"
equal(t, got, want)
}
func getCompiled(t *testing.T, file string) string { func getCompiled(t *testing.T, file string) string {
code, err := ioutil.ReadFile(file) code, err := ioutil.ReadFile(file)

View File

@@ -41,7 +41,9 @@ var keywords = []string{
"false", "false",
"case", "case",
"default", "default",
"return"} "return",
"try",
"catch"}
var whitespace = []byte{' ', '\n', '\t'} var whitespace = []byte{' ', '\n', '\t'}
var identifier = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" var identifier = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"

View File

@@ -19,7 +19,7 @@ func usage() {
func main() { func main() {
// read test file // read test file
code, _ := ioutil.ReadFile("in/complex.asl") code, _ := ioutil.ReadFile("in/simple.asl")
token := asl.Tokenize(code) token := asl.Tokenize(code)
out := asl.Parse(token, true) out := asl.Parse(token, true)

View File

@@ -0,0 +1,5 @@
try {
// ...
} catch {
// ...
}