mirror of
https://github.com/Kugelschieber/asl.git
synced 2026-01-18 12:00:25 +00:00
Basic parser, fixed bug in tokenizer.
This commit is contained in:
@@ -1,2 +1,9 @@
|
||||
var a = 1;
|
||||
var b = 2;
|
||||
var _a = 1;
|
||||
var _b = 2;
|
||||
|
||||
if _a < _b {
|
||||
var _x = 5;
|
||||
}
|
||||
else{
|
||||
var _x = 6;
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -4,4 +4,126 @@ import (
|
||||
|
||||
)
|
||||
|
||||
const TAB = " "
|
||||
|
||||
var tokens []Token
|
||||
var tokenIndex int
|
||||
var out string
|
||||
var offset int
|
||||
|
||||
func Parse(token []Token) string {
|
||||
initParser(token)
|
||||
|
||||
for tokenIndex < len(token) {
|
||||
parseBlock()
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// parser functions
|
||||
|
||||
func parseBlock() {
|
||||
if get().token == "var" {
|
||||
parseVar()
|
||||
} else if get().token == "if" {
|
||||
parseIf()
|
||||
} else {
|
||||
parseStatement()
|
||||
}
|
||||
}
|
||||
|
||||
func parseVar() {
|
||||
expect("var")
|
||||
appendOut(get().token)
|
||||
next()
|
||||
|
||||
if accept("=") {
|
||||
next()
|
||||
appendOut(" = "+get().token)
|
||||
next()
|
||||
}
|
||||
|
||||
appendOut(";\n")
|
||||
expect(";")
|
||||
}
|
||||
|
||||
func parseIf() {
|
||||
expect("if")
|
||||
appendOut("if (")
|
||||
parseCondition()
|
||||
appendOut(") then {\n")
|
||||
expect("{")
|
||||
parseBlock()
|
||||
expect("}")
|
||||
|
||||
if accept("else") {
|
||||
next()
|
||||
expect("{")
|
||||
appendOut("} else {\n")
|
||||
parseBlock()
|
||||
expect("}")
|
||||
}
|
||||
|
||||
appendOut("};")
|
||||
}
|
||||
|
||||
func parseCondition() {
|
||||
for get().token != "{" {
|
||||
appendOut(get().token)
|
||||
next()
|
||||
|
||||
if get().token != "{" {
|
||||
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+"'")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -8,8 +8,17 @@ type Token struct{
|
||||
token string
|
||||
}
|
||||
|
||||
var delimiter = []byte{'=', ';'}
|
||||
var keywords = []string{"var"}
|
||||
var delimiter = []byte{'=',
|
||||
';',
|
||||
'{',
|
||||
'}',
|
||||
'<',
|
||||
'>',
|
||||
'!'}
|
||||
|
||||
var keywords = []string{"var",
|
||||
"if"}
|
||||
|
||||
var whitespace = []byte{' ', '\n', '\t'}
|
||||
|
||||
func Tokenize(code []byte) []Token {
|
||||
@@ -20,7 +29,10 @@ func Tokenize(code []byte) []Token {
|
||||
c := code[i]
|
||||
|
||||
if byteArrayContains(delimiter, c) {
|
||||
tokens = append(tokens, Token{token})
|
||||
if token != "" {
|
||||
tokens = append(tokens, Token{token})
|
||||
}
|
||||
|
||||
tokens = append(tokens, Token{string(c)})
|
||||
token = ""
|
||||
} else if stringArrayContains(keywords, token) {
|
||||
@@ -32,9 +44,11 @@ func Tokenize(code []byte) []Token {
|
||||
}
|
||||
|
||||
// TEST
|
||||
fmt.Println("Tokens:")
|
||||
for i := range tokens {
|
||||
fmt.Println(tokens[i].token)
|
||||
}
|
||||
fmt.Println("---")
|
||||
|
||||
return tokens
|
||||
}
|
||||
|
||||
@@ -3,10 +3,14 @@ package main
|
||||
import (
|
||||
"io/ioutil"
|
||||
"asl"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main(){
|
||||
// read test file
|
||||
code, _ := ioutil.ReadFile("in/simple.asl")
|
||||
asl.Tokenize(code)
|
||||
token := asl.Tokenize(code)
|
||||
out := asl.Parse(token)
|
||||
|
||||
fmt.Println(out)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user