mirror of
https://github.com/Kugelschieber/asl.git
synced 2026-01-18 12:00:25 +00:00
Function parsing, some fixes to parser.
This commit is contained in:
3
in/func.asl
Normal file
3
in/func.asl
Normal file
@@ -0,0 +1,3 @@
|
||||
func somefunc() {
|
||||
|
||||
}
|
||||
@@ -1,9 +1,17 @@
|
||||
var _a = 1;
|
||||
var _b = 2;
|
||||
|
||||
func somefunc(_x, _y, _z) {
|
||||
if _x < _y {
|
||||
hint(_z);
|
||||
}
|
||||
}
|
||||
|
||||
if _a < _b {
|
||||
var _x = 5;
|
||||
somefunc(1, "two", 3);
|
||||
}
|
||||
else{
|
||||
var _x = 6;
|
||||
_a = 3;
|
||||
}
|
||||
|
||||
hint("this is a hint");
|
||||
|
||||
Binary file not shown.
@@ -1,16 +1,11 @@
|
||||
package asl
|
||||
|
||||
import (
|
||||
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const TAB = " "
|
||||
|
||||
var tokens []Token
|
||||
var tokenIndex int
|
||||
var out string
|
||||
var offset int
|
||||
|
||||
func Parse(token []Token) string {
|
||||
initParser(token)
|
||||
|
||||
@@ -21,13 +16,13 @@ func Parse(token []Token) string {
|
||||
return out
|
||||
}
|
||||
|
||||
// parser functions
|
||||
|
||||
func parseBlock() {
|
||||
if get().token == "var" {
|
||||
if accept("var") {
|
||||
parseVar()
|
||||
} else if get().token == "if" {
|
||||
} else if accept("if") {
|
||||
parseIf()
|
||||
} else if accept("func") {
|
||||
parseFunction()
|
||||
} else {
|
||||
parseStatement()
|
||||
}
|
||||
@@ -65,65 +60,101 @@ func parseIf() {
|
||||
expect("}")
|
||||
}
|
||||
|
||||
appendOut("};")
|
||||
appendOut("};\n")
|
||||
}
|
||||
|
||||
func parseCondition() {
|
||||
for get().token != "{" {
|
||||
for !accept("{") {
|
||||
appendOut(get().token)
|
||||
next()
|
||||
|
||||
if get().token != "{" {
|
||||
if !accept("{") {
|
||||
appendOut(" ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func parseStatement() {
|
||||
|
||||
}
|
||||
|
||||
// helper functions
|
||||
|
||||
func initParser(token []Token) {
|
||||
if len(token) == 0 {
|
||||
panic("No tokens provided")
|
||||
}
|
||||
|
||||
tokens = token
|
||||
tokenIndex = 0
|
||||
out = ""
|
||||
offset = 0
|
||||
}
|
||||
|
||||
func accept(token string) bool {
|
||||
return tokenEqual(token, get())
|
||||
}
|
||||
|
||||
func expect(token string) {
|
||||
if !tokenEqual(token, get()) {
|
||||
panic("Parse error, expected '"+token+"' but was '"+get().token+"'")
|
||||
}
|
||||
|
||||
func parseFunction() {
|
||||
expect("func")
|
||||
appendOut(get().token+" = {\n")
|
||||
next()
|
||||
expect("(")
|
||||
parseFunctionParameter()
|
||||
expect(")")
|
||||
expect("{")
|
||||
parseBlock()
|
||||
expect("}")
|
||||
appendOut("};\n")
|
||||
}
|
||||
|
||||
func next() {
|
||||
tokenIndex++
|
||||
}
|
||||
|
||||
func get() Token {
|
||||
if tokenIndex >= len(tokens) {
|
||||
panic("No more tokens")
|
||||
func parseFunctionParameter() {
|
||||
// empty parameter list
|
||||
if accept(")") {
|
||||
return;
|
||||
}
|
||||
|
||||
return tokens[tokenIndex]
|
||||
i := int64(0)
|
||||
|
||||
for !accept(")") {
|
||||
name := get().token
|
||||
next()
|
||||
appendOut(name+" = this select "+strconv.FormatInt(i, 10)+";\n")
|
||||
i++
|
||||
|
||||
if !accept(")") {
|
||||
expect(",")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func tokenEqual(a string, b Token) bool {
|
||||
return a == b.token
|
||||
// Everything that does not start with a keyword.
|
||||
func parseStatement() {
|
||||
// empty block
|
||||
if accept("}") {
|
||||
return;
|
||||
}
|
||||
|
||||
// variable or function name
|
||||
name := get().token
|
||||
next()
|
||||
|
||||
if accept("=") {
|
||||
appendOut(name)
|
||||
parseAssignment()
|
||||
} else {
|
||||
parseFunctionCall()
|
||||
appendOut(name+";\n")
|
||||
}
|
||||
}
|
||||
|
||||
func appendOut(str string) {
|
||||
out += str
|
||||
func parseAssignment() {
|
||||
expect("=")
|
||||
appendOut(" = "+get().token)
|
||||
next()
|
||||
expect(";")
|
||||
appendOut(";\n")
|
||||
}
|
||||
|
||||
func parseFunctionCall() {
|
||||
expect("(")
|
||||
params := parseParameter()
|
||||
expect(")")
|
||||
expect(";")
|
||||
appendOut("["+params+"] call ")
|
||||
}
|
||||
|
||||
func parseParameter() string {
|
||||
params := ""
|
||||
|
||||
for !accept(")") {
|
||||
params += get().token
|
||||
next()
|
||||
|
||||
if !accept(")") {
|
||||
expect(",")
|
||||
params += ", "
|
||||
}
|
||||
}
|
||||
|
||||
return params
|
||||
}
|
||||
|
||||
49
src/asl/parserHelper.go
Normal file
49
src/asl/parserHelper.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package asl
|
||||
|
||||
var tokens []Token
|
||||
var tokenIndex int
|
||||
var out string
|
||||
var offset int
|
||||
|
||||
func initParser(token []Token) {
|
||||
if len(token) == 0 {
|
||||
panic("No tokens provided")
|
||||
}
|
||||
|
||||
tokens = token
|
||||
tokenIndex = 0
|
||||
out = ""
|
||||
offset = 0
|
||||
}
|
||||
|
||||
func accept(token string) bool {
|
||||
return tokenEqual(token, get())
|
||||
}
|
||||
|
||||
func expect(token string) {
|
||||
if !tokenEqual(token, get()) {
|
||||
panic("Parse error, expected '"+token+"' but was '"+get().token+"'")
|
||||
}
|
||||
|
||||
next()
|
||||
}
|
||||
|
||||
func next() {
|
||||
tokenIndex++
|
||||
}
|
||||
|
||||
func get() Token {
|
||||
if tokenIndex >= len(tokens) {
|
||||
panic("No more tokens")
|
||||
}
|
||||
|
||||
return tokens[tokenIndex]
|
||||
}
|
||||
|
||||
func tokenEqual(a string, b Token) bool {
|
||||
return a == b.token
|
||||
}
|
||||
|
||||
func appendOut(str string) {
|
||||
out += str
|
||||
}
|
||||
@@ -12,12 +12,16 @@ var delimiter = []byte{'=',
|
||||
';',
|
||||
'{',
|
||||
'}',
|
||||
'(',
|
||||
')',
|
||||
'<',
|
||||
'>',
|
||||
'!'}
|
||||
'!',
|
||||
','}
|
||||
|
||||
var keywords = []string{"var",
|
||||
"if"}
|
||||
"if",
|
||||
"func"}
|
||||
|
||||
var whitespace = []byte{' ', '\n', '\t'}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user