Implemented runes
This commit is contained in:
parent
e91e894046
commit
21017e6378
@ -2,12 +2,13 @@ import mem
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
length := 4
|
length := 5
|
||||||
address := mem.alloc(length)
|
address := mem.alloc(length)
|
||||||
address[0] = 65
|
address[0] = 'H'
|
||||||
address[1] = 66
|
address[1] = 'e'
|
||||||
address[2] = 67
|
address[2] = 'l'
|
||||||
address[3] = 68
|
address[3] = 'l'
|
||||||
|
address[4] = 'o'
|
||||||
sys.write(1, address, length)
|
sys.write(1, address, length)
|
||||||
mem.free(address, length)
|
mem.free(address, length)
|
||||||
}
|
}
|
@ -1,8 +1,6 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"git.akyoto.dev/cli/q/src/build/asm"
|
"git.akyoto.dev/cli/q/src/build/asm"
|
||||||
"git.akyoto.dev/cli/q/src/build/ast"
|
"git.akyoto.dev/cli/q/src/build/ast"
|
||||||
"git.akyoto.dev/cli/q/src/build/errors"
|
"git.akyoto.dev/cli/q/src/build/errors"
|
||||||
@ -38,19 +36,25 @@ func (f *Function) CompileAssign(node *ast.Assign) error {
|
|||||||
defer f.useVariable(variable)
|
defer f.useVariable(variable)
|
||||||
|
|
||||||
index := left.Children[1]
|
index := left.Children[1]
|
||||||
offset, err := strconv.Atoi(index.Token.Text(f.File.Bytes))
|
offset, _, err := f.Number(index.Token)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
num, err := strconv.Atoi(right.Token.Text(f.File.Bytes))
|
number, size, err := f.Number(right.Token)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
f.MemoryNumber(asm.STORE, asm.Memory{Base: variable.Register, Offset: byte(offset), Length: 1}, num)
|
elementSize := byte(1)
|
||||||
|
|
||||||
|
if size != elementSize {
|
||||||
|
return errors.New(&errors.NumberExceedsBounds{Number: number, ExpectedSize: elementSize, Size: size}, f.File, right.Token.Position)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.MemoryNumber(asm.STORE, asm.Memory{Base: variable.Register, Offset: byte(offset), Length: elementSize}, number)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"git.akyoto.dev/cli/q/src/build/cpu"
|
"git.akyoto.dev/cli/q/src/build/cpu"
|
||||||
"git.akyoto.dev/cli/q/src/build/errors"
|
"git.akyoto.dev/cli/q/src/build/errors"
|
||||||
"git.akyoto.dev/cli/q/src/build/token"
|
"git.akyoto.dev/cli/q/src/build/token"
|
||||||
@ -22,9 +20,8 @@ func (f *Function) ExecuteLeaf(operation token.Token, register cpu.Register, ope
|
|||||||
defer f.useVariable(variable)
|
defer f.useVariable(variable)
|
||||||
return f.ExecuteRegisterRegister(operation, register, variable.Register)
|
return f.ExecuteRegisterRegister(operation, register, variable.Register)
|
||||||
|
|
||||||
case token.Number:
|
case token.Number, token.Rune:
|
||||||
value := operand.Text(f.File.Bytes)
|
number, _, err := f.Number(operand)
|
||||||
number, err := strconv.Atoi(value)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
36
src/build/core/Number.go
Normal file
36
src/build/core/Number.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"git.akyoto.dev/cli/q/src/build/errors"
|
||||||
|
"git.akyoto.dev/cli/q/src/build/token"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Number tries to convert the token into a numeric value.
|
||||||
|
func (f *Function) Number(t token.Token) (int, byte, error) {
|
||||||
|
switch t.Kind {
|
||||||
|
case token.Number:
|
||||||
|
number, err := strconv.Atoi(t.Text(f.File.Bytes))
|
||||||
|
return number, 8, err
|
||||||
|
|
||||||
|
case token.Rune:
|
||||||
|
r := t.Bytes(f.File.Bytes)
|
||||||
|
r = r[1 : len(r)-1]
|
||||||
|
|
||||||
|
if len(r) == 0 {
|
||||||
|
return 0, 0, errors.New(errors.InvalidRune, f.File, t.Position+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
number, size := utf8.DecodeRune(r)
|
||||||
|
|
||||||
|
if len(r) > size {
|
||||||
|
return 0, 0, errors.New(errors.InvalidRune, f.File, t.Position+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return int(number), byte(size), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, 0, errors.New(errors.InvalidNumber, f.File, t.Position)
|
||||||
|
}
|
@ -2,7 +2,6 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"git.akyoto.dev/cli/q/src/build/asm"
|
"git.akyoto.dev/cli/q/src/build/asm"
|
||||||
"git.akyoto.dev/cli/q/src/build/cpu"
|
"git.akyoto.dev/cli/q/src/build/cpu"
|
||||||
@ -26,15 +25,14 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error {
|
|||||||
f.RegisterRegister(asm.MOVE, register, variable.Register)
|
f.RegisterRegister(asm.MOVE, register, variable.Register)
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case token.Number:
|
case token.Number, token.Rune:
|
||||||
value := t.Text(f.File.Bytes)
|
number, _, err := f.Number(t)
|
||||||
n, err := strconv.Atoi(value)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
f.RegisterNumber(asm.MOVE, register, n)
|
f.RegisterNumber(asm.MOVE, register, number)
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case token.String:
|
case token.String:
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package errors
|
package errors
|
||||||
|
|
||||||
var (
|
var (
|
||||||
InvalidStatement = &Base{"Invalid statement"}
|
InvalidNumber = &Base{"Invalid number"}
|
||||||
InvalidExpression = &Base{"Invalid expression"}
|
InvalidExpression = &Base{"Invalid expression"}
|
||||||
MissingOperand = &Base{"Missing operand"}
|
InvalidRune = &Base{"Invalid rune"}
|
||||||
|
InvalidStatement = &Base{"Invalid statement"}
|
||||||
MissingMainFunction = &Base{"Missing main function"}
|
MissingMainFunction = &Base{"Missing main function"}
|
||||||
|
MissingOperand = &Base{"Missing operand"}
|
||||||
NotImplemented = &Base{"Not implemented"}
|
NotImplemented = &Base{"Not implemented"}
|
||||||
)
|
)
|
||||||
|
15
src/build/errors/NumberExceedsBounds.go
Normal file
15
src/build/errors/NumberExceedsBounds.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// NumberExceedsBounds error is created when the number doesn't fit into the destination.
|
||||||
|
type NumberExceedsBounds struct {
|
||||||
|
Number int
|
||||||
|
Size byte
|
||||||
|
ExpectedSize byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error generates the string representation.
|
||||||
|
func (err *NumberExceedsBounds) Error() string {
|
||||||
|
return fmt.Sprintf("Number %d needs %d bytes but the maximum is %d bytes", err.Number, err.Size, err.ExpectedSize)
|
||||||
|
}
|
@ -89,7 +89,7 @@ func Parse(tokens []token.Token) *Expression {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.Kind == token.Identifier || t.Kind == token.Number || t.Kind == token.String {
|
if t.Kind == token.Identifier || t.Kind == token.Number || t.Kind == token.String || t.Kind == token.Rune {
|
||||||
if cursor != nil {
|
if cursor != nil {
|
||||||
node := NewLeaf(t)
|
node := NewLeaf(t)
|
||||||
cursor.AddChild(node)
|
cursor.AddChild(node)
|
||||||
|
@ -9,6 +9,7 @@ const (
|
|||||||
NewLine // NewLine is the newline character.
|
NewLine // NewLine is the newline character.
|
||||||
Identifier // Identifier is a series of characters used to identify a variable or function.
|
Identifier // Identifier is a series of characters used to identify a variable or function.
|
||||||
Number // Number is a series of numerical characters.
|
Number // Number is a series of numerical characters.
|
||||||
|
Rune // Rune is a single unicode code point.
|
||||||
String // String is an uninterpreted series of characters in the source code.
|
String // String is an uninterpreted series of characters in the source code.
|
||||||
Comment // Comment is a comment.
|
Comment // Comment is a comment.
|
||||||
Separator // ,
|
Separator // ,
|
||||||
|
@ -48,13 +48,14 @@ func Tokenize(buffer []byte) List {
|
|||||||
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
case '"':
|
case '"', '\'':
|
||||||
|
limiter := buffer[i]
|
||||||
start := i
|
start := i
|
||||||
end := Position(len(buffer))
|
end := Position(len(buffer))
|
||||||
i++
|
i++
|
||||||
|
|
||||||
for i < Position(len(buffer)) {
|
for i < Position(len(buffer)) {
|
||||||
if buffer[i] == '"' {
|
if buffer[i] == limiter {
|
||||||
end = i + 1
|
end = i + 1
|
||||||
i++
|
i++
|
||||||
break
|
break
|
||||||
@ -63,7 +64,13 @@ func Tokenize(buffer []byte) List {
|
|||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
tokens = append(tokens, Token{Kind: String, Position: start, Length: Length(end - start)})
|
kind := String
|
||||||
|
|
||||||
|
if limiter == '\'' {
|
||||||
|
kind = Rune
|
||||||
|
}
|
||||||
|
|
||||||
|
tokens = append(tokens, Token{Kind: kind, Position: start, Length: Length(end - start)})
|
||||||
continue
|
continue
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -17,7 +17,7 @@ var examples = []struct {
|
|||||||
{"hello", "", "Hello", 0},
|
{"hello", "", "Hello", 0},
|
||||||
{"factorial", "", "", 120},
|
{"factorial", "", "", 120},
|
||||||
{"fibonacci", "", "", 55},
|
{"fibonacci", "", "", 55},
|
||||||
{"array", "", "ABCD", 0},
|
{"array", "", "Hello", 0},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExamples(t *testing.T) {
|
func TestExamples(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user