Added short form for variable definitions

This commit is contained in:
Eduard Urbach 2024-06-14 18:03:54 +02:00
parent 6de8ac7b9f
commit 65791ea5a1
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
6 changed files with 76 additions and 33 deletions

View File

@ -24,6 +24,12 @@ Build a Linux ELF executable from `examples/hello`:
./examples/hello/hello ./examples/hello/hello
``` ```
To produce verbose output, add the `-v` flag:
```shell
./q build examples/hello -v
```
## Documentation ## Documentation
### [main.go](main.go) ### [main.go](main.go)

View File

@ -1,7 +1,7 @@
main() { main() {
let write = 1 write := 1
let exit = 60 exit := 60
let stdout = 1 stdout := 1
syscall(write, stdout, 4194305, 3) syscall(write, stdout, 4194305, 3)
syscall(exit, 0) syscall(exit, 0)

View File

@ -18,6 +18,7 @@ type Function struct {
Body token.List Body token.List
Variables map[string]*Variable Variables map[string]*Variable
Assembler asm.Assembler Assembler asm.Assembler
Error error
} }
// Compile turns a function into machine code. // Compile turns a function into machine code.
@ -27,35 +28,53 @@ func (f *Function) Compile() {
} }
for _, line := range f.Lines() { for _, line := range f.Lines() {
if config.Verbose {
fmt.Println("[line]", line)
}
if len(line) == 0 { if len(line) == 0 {
continue continue
} }
if line[0].Kind == token.Keyword { if config.Verbose {
fmt.Println("[line]", line)
}
err := f.compileInstruction(line)
if err != nil {
ansi.Red.Println(err)
return
}
}
f.Assembler.Return()
}
// compileInstruction compiles a single instruction.
func (f *Function) compileInstruction(line token.List) error {
switch line[0].Kind {
case token.Keyword:
switch line[0].Text() { switch line[0].Text() {
case "let": case "return":
name := line[1].Text() f.Assembler.Return()
value := line[3:] }
case token.Identifier:
if len(line) >= 2 && line[1].Kind == token.Operator && line[1].Text() == ":=" {
name := line[0].Text()
value := line[2:]
if config.Verbose { if config.Verbose {
fmt.Println("[variable]", name, value) fmt.Println("[variable]", name, value)
} }
f.Variables[name] = &Variable{ f.Variables[name] = &Variable{
Value: line[3:], Value: line[2:],
IsConst: true, IsConst: true,
} }
case "return": return nil
f.Assembler.Return()
}
} }
if line[0].Kind == token.Identifier && line[0].Text() == "syscall" { switch line[0].Text() {
case "syscall":
paramTokens := line[2 : len(line)-1] paramTokens := line[2 : len(line)-1]
start := 0 start := 0
i := 0 i := 0
@ -78,7 +97,11 @@ func (f *Function) Compile() {
switch list[0].Kind { switch list[0].Kind {
case token.Identifier: case token.Identifier:
name := list[0].Text() name := list[0].Text()
variable := f.Variables[name] variable, exists := f.Variables[name]
if !exists {
return fmt.Errorf("Unknown identifier '%s'", name)
}
if !variable.IsConst { if !variable.IsConst {
panic("Not implemented yet") panic("Not implemented yet")
@ -101,7 +124,7 @@ func (f *Function) Compile() {
} }
} }
f.Assembler.Return() return nil
} }
// Lines returns the lines in the function body. // Lines returns the lines in the function body.

View File

@ -2,6 +2,5 @@ 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 last.Kind == Keyword || last.Kind == Separator { if last.Kind == Keyword || last.Kind == Separator || last.Kind == Operator || t.Kind == Operator {
builder.WriteByte(' ') builder.WriteByte(' ')
} }

View File

@ -70,9 +70,6 @@ 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})
@ -122,6 +119,24 @@ func Tokenize(buffer []byte) List {
continue continue
} }
// Operators
if isOperator(buffer[i]) {
position := i
i++
for i < len(buffer) && isOperator(buffer[i]) {
i++
}
tokens = append(tokens, Token{
Operator,
position,
buffer[position:i],
})
continue
}
} }
i++ i++