Init, basic tokenizer setup.

This commit is contained in:
Marvin Blum
2015-09-21 13:26:17 +02:00
commit 651c7451d1
9 changed files with 108 additions and 0 deletions

7
src/asl/parser.go Normal file
View File

@@ -0,0 +1,7 @@
package asl
import (
)

60
src/asl/tokenizer.go Normal file
View File

@@ -0,0 +1,60 @@
package asl
import (
"fmt"
)
type Token struct{
token string
}
var delimiter = []byte{'=', ';'}
var keywords = []string{"var"}
var whitespace = []byte{' ', '\n', '\t'}
func Tokenize(code []byte) []Token {
tokens := make([]Token, 0)
token := ""
for i := range code {
c := code[i]
if byteArrayContains(delimiter, c) {
tokens = append(tokens, Token{token})
tokens = append(tokens, Token{string(c)})
token = ""
} else if stringArrayContains(keywords, token) {
tokens = append(tokens, Token{token})
token = ""
} else if !byteArrayContains(whitespace, c) {
token += string(c)
}
}
// TEST
for i := range tokens {
fmt.Println(tokens[i].token)
}
return tokens
}
func byteArrayContains(haystack []byte, needle byte) bool {
for i := range haystack {
if haystack[i] == needle {
return true;
}
}
return false
}
func stringArrayContains(haystack []string, needle string) bool {
for i := range haystack {
if haystack[i] == needle {
return true;
}
}
return false
}

12
src/main/asl.go Normal file
View File

@@ -0,0 +1,12 @@
package main
import (
"io/ioutil"
"asl"
)
func main(){
// read test file
code, _ := ioutil.ReadFile("in/simple.asl")
asl.Tokenize(code)
}