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));~~
|
* ~~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
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
// TODO:
|
try {
|
||||||
//var _x = setHitIndex(vehicle()(player))(1, 1);
|
var x = 100;
|
||||||
|
doSomething(x);
|
||||||
|
} catch {
|
||||||
|
errorHandling();
|
||||||
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
|
|||||||
@@ -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_"
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
|
|||||||
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