mirror of
https://github.com/Kugelschieber/asl.git
synced 2026-01-18 12:00:25 +00:00
Added comments to preprocessor.
This commit is contained in:
@@ -33,6 +33,8 @@ func parseBlock() {
|
||||
parseFunction()
|
||||
} else if accept("return") {
|
||||
parseReturn()
|
||||
} else if accept("sqf") {
|
||||
parseSqf()
|
||||
} else {
|
||||
parseStatement()
|
||||
}
|
||||
@@ -206,6 +208,19 @@ func parseReturn() {
|
||||
appendOut(";\n")
|
||||
}
|
||||
|
||||
func parseSqf() {
|
||||
expect("sqf")
|
||||
expect(":")
|
||||
|
||||
for !accept("sqf") {
|
||||
appendOut(get().token)
|
||||
next()
|
||||
}
|
||||
|
||||
appendOut("\n")
|
||||
expect("sqf")
|
||||
}
|
||||
|
||||
// Everything that does not start with a keyword.
|
||||
func parseStatement() {
|
||||
// empty block
|
||||
|
||||
@@ -2,6 +2,7 @@ package asl
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Token struct{
|
||||
@@ -36,14 +37,19 @@ var keywords = []string{
|
||||
"false",
|
||||
"case",
|
||||
"default",
|
||||
"return"}
|
||||
"return",
|
||||
"sqfstart",
|
||||
"sqf"}
|
||||
|
||||
var whitespace = []byte{' ', '\n', '\t'}
|
||||
|
||||
func Tokenize(code []byte) []Token {
|
||||
code = removeComments(code)
|
||||
tokens := make([]Token, 0)
|
||||
token := ""
|
||||
|
||||
fmt.Println(string(code))
|
||||
|
||||
for i := range code {
|
||||
c := code[i]
|
||||
|
||||
@@ -65,6 +71,54 @@ func Tokenize(code []byte) []Token {
|
||||
return tokens
|
||||
}
|
||||
|
||||
func removeComments(code []byte) []byte {
|
||||
newcode := make([]byte, len(code))
|
||||
j := 0
|
||||
|
||||
for i := 0; i < len(code); i++ {
|
||||
c := code[i]
|
||||
|
||||
if c == '/' && nextChar(code, i) == '/' {
|
||||
i = skipSingleLineComment(code, i+1)
|
||||
continue
|
||||
} else if c == '/' && nextChar(code, i) == '*' {
|
||||
i = skipMultiLineComment(code, i+1)
|
||||
continue
|
||||
}
|
||||
|
||||
newcode[j] = c
|
||||
j++
|
||||
}
|
||||
|
||||
return newcode[:j]
|
||||
}
|
||||
|
||||
func nextChar(code []byte, i int) byte {
|
||||
i++
|
||||
|
||||
if i < len(code) {
|
||||
return code[i]
|
||||
}
|
||||
|
||||
return '0'
|
||||
}
|
||||
|
||||
func skipSingleLineComment(code []byte, i int) int {
|
||||
for i < len(code) && code[i] != '\n' {
|
||||
i++
|
||||
}
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
func skipMultiLineComment(code []byte, i int) int {
|
||||
for i < len(code) && !(code[i] == '*' && nextChar(code, i) == '/') {
|
||||
i++
|
||||
}
|
||||
|
||||
return i+1
|
||||
}
|
||||
|
||||
func byteArrayContains(haystack []byte, needle byte) bool {
|
||||
for i := range haystack {
|
||||
if haystack[i] == needle {
|
||||
|
||||
Reference in New Issue
Block a user