mirror of
https://github.com/Kugelschieber/asl.git
synced 2026-01-18 12:00:25 +00:00
Added try ... catch.
This commit is contained in:
2
ToDo.md
2
ToDo.md
@@ -10,7 +10,7 @@
|
||||
* ~~inline buildin function call -> foo(a)(bar(x)(y));~~
|
||||
* ~~negative values e.g. -1, operator !~~
|
||||
* ~~tokenizer splits commands like "format" -> for, mat~~
|
||||
* try ... catch
|
||||
* ~~try ... catch~~
|
||||
|
||||
## Special cases
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// TODO:
|
||||
//var _x = setHitIndex(vehicle()(player))(1, 1);
|
||||
|
||||
try {
|
||||
var x = 100;
|
||||
doSomething(x);
|
||||
} catch {
|
||||
errorHandling();
|
||||
}
|
||||
|
||||
@@ -35,6 +35,8 @@ func parseBlock() {
|
||||
parseFunction()
|
||||
} else if accept("return") {
|
||||
parseReturn()
|
||||
} else if accept("try") {
|
||||
parseTryCatch()
|
||||
} else if accept("case") || accept("default") {
|
||||
return
|
||||
} else {
|
||||
@@ -232,6 +234,20 @@ func parseReturn() {
|
||||
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.
|
||||
func parseStatement() {
|
||||
// empty block
|
||||
|
||||
@@ -101,6 +101,13 @@ func TestParserOperator(t *testing.T) {
|
||||
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 {
|
||||
code, err := ioutil.ReadFile(file)
|
||||
|
||||
|
||||
@@ -41,7 +41,9 @@ var keywords = []string{
|
||||
"false",
|
||||
"case",
|
||||
"default",
|
||||
"return"}
|
||||
"return",
|
||||
"try",
|
||||
"catch"}
|
||||
|
||||
var whitespace = []byte{' ', '\n', '\t'}
|
||||
var identifier = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
|
||||
|
||||
@@ -19,7 +19,7 @@ func usage() {
|
||||
|
||||
func main() {
|
||||
// read test file
|
||||
code, _ := ioutil.ReadFile("in/complex.asl")
|
||||
code, _ := ioutil.ReadFile("in/simple.asl")
|
||||
token := asl.Tokenize(code)
|
||||
out := asl.Parse(token, true)
|
||||
|
||||
|
||||
5
test/parser_try_catch.asl
Normal file
5
test/parser_try_catch.asl
Normal file
@@ -0,0 +1,5 @@
|
||||
try {
|
||||
// ...
|
||||
} catch {
|
||||
// ...
|
||||
}
|
||||
Reference in New Issue
Block a user