Implemented numeric constants

This commit is contained in:
Eduard Urbach 2024-06-14 11:48:28 +02:00
parent f04e9d7a60
commit 19489d7a9a
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
7 changed files with 59 additions and 9 deletions

View File

@ -1,4 +1,8 @@
main() { main() {
syscall(1, 1, 4194305, 3) let write = 1
syscall(60, 0) let exit = 60
let stdout = 1
syscall(write, stdout, 4194305, 3)
syscall(exit, 0)
} }

View File

@ -16,6 +16,7 @@ type Function struct {
Name string Name string
Head token.List Head token.List
Body token.List Body token.List
Variables map[string]*Variable
Assembler asm.Assembler Assembler asm.Assembler
} }
@ -36,6 +37,16 @@ func (f *Function) Compile() {
if line[0].Kind == token.Keyword { if line[0].Kind == token.Keyword {
switch line[0].Text() { switch line[0].Text() {
case "let":
name := line[1].Text()
value := line[3:]
fmt.Println("[variable]", name, value)
f.Variables[name] = &Variable{
Value: line[3:],
IsConst: true,
}
case "return": case "return":
f.Assembler.Return() f.Assembler.Return()
} }
@ -61,16 +72,33 @@ func (f *Function) Compile() {
} }
for i, list := range parameters { for i, list := range parameters {
if list[0].Kind == token.Number { switch list[0].Kind {
numAsText := list[0].Text() case token.Identifier:
n, _ := strconv.Atoi(numAsText) name := list[0].Text()
variable := f.Variables[name]
if !variable.IsConst {
panic("Not implemented yet")
}
n, _ := strconv.Atoi(variable.Value[0].Text())
f.Assembler.MoveRegisterNumber(x64.SyscallArgs[i], uint64(n)) f.Assembler.MoveRegisterNumber(x64.SyscallArgs[i], uint64(n))
case token.Number:
value := list[0].Text()
n, _ := strconv.Atoi(value)
f.Assembler.MoveRegisterNumber(x64.SyscallArgs[i], uint64(n))
default:
panic("Unknown expression")
} }
} }
f.Assembler.Syscall() f.Assembler.Syscall()
} }
} }
f.Assembler.Return()
} }
// Lines returns the lines in the function body. // Lines returns the lines in the function body.

View File

@ -218,6 +218,7 @@ func scanFile(path string, functions chan<- *Function) error {
Name: tokens[nameStart].Text(), Name: tokens[nameStart].Text(),
Head: tokens[paramsStart:bodyStart], Head: tokens[paramsStart:bodyStart],
Body: tokens[bodyStart : i+1], Body: tokens[bodyStart : i+1],
Variables: map[string]*Variable{},
} }
nameStart = -1 nameStart = -1

9
src/build/Variable.go Normal file
View File

@ -0,0 +1,9 @@
package build
import "git.akyoto.dev/cli/q/src/build/token"
// Variable represents a variable in a function.
type Variable struct {
Value token.List
IsConst bool
}

View File

@ -2,5 +2,6 @@ package token
// Keywords defines the keywords used in the language. // Keywords defines the keywords used in the language.
var Keywords = map[string]bool{ var Keywords = map[string]bool{
"let": true,
"return": true, "return": true,
} }

View File

@ -13,7 +13,7 @@ func (list List) String() string {
var last Token var last Token
for _, t := range list { for _, t := range list {
if t.Kind == Identifier && last.Kind == Separator { if last.Kind == Keyword || last.Kind == Separator {
builder.WriteByte(' ') builder.WriteByte(' ')
} }

View File

@ -70,6 +70,9 @@ func Tokenize(buffer []byte) List {
case ']': case ']':
tokens = append(tokens, Token{ArrayEnd, i, arrayEndBytes}) tokens = append(tokens, Token{ArrayEnd, i, arrayEndBytes})
case '=', ':', '+', '-', '*', '/', '<', '>', '!':
tokens = append(tokens, Token{Operator, i, buffer[i : i+1]})
// Separator // Separator
case ',': case ',':
tokens = append(tokens, Token{Separator, i, separatorBytes}) tokens = append(tokens, Token{Separator, i, separatorBytes})
@ -147,3 +150,7 @@ func isNumber(c byte) bool {
func isNumberStart(c byte) bool { func isNumberStart(c byte) bool {
return isNumber(c) || c == '-' return isNumber(c) || c == '-'
} }
func isOperator(c byte) bool {
return c == '=' || c == ':' || c == '+' || c == '-' || c == '*' || c == '/' || c == '<' || c == '>' || c == '!'
}