Improved code quality

This commit is contained in:
2025-02-20 16:55:17 +01:00
parent 9779476fe7
commit 36d0142573
12 changed files with 51 additions and 37 deletions

View File

@ -29,7 +29,8 @@ func (f *Function) ArrayElementToRegister(node *expression.Expression, register
Length: byte(1),
}
if index.Token.IsNumeric() {
switch {
case index.Token.IsNumeric():
offset, err := f.ToNumber(index.Token)
if err != nil {
@ -38,7 +39,7 @@ func (f *Function) ArrayElementToRegister(node *expression.Expression, register
memory.Offset = int8(offset)
} else if index.Token.Kind == token.Identifier {
case index.Token.Kind == token.Identifier:
indexName := index.Token.Text(f.File.Bytes)
indexVariable := f.VariableByName(indexName)
@ -53,7 +54,8 @@ func (f *Function) ArrayElementToRegister(node *expression.Expression, register
}
memory.OffsetRegister = indexVariable.Register
} else {
default:
typ, err := f.ExpressionToRegister(index, register)
if err != nil {

View File

@ -21,7 +21,7 @@ func (f *Function) CompileDelete(root *expression.Expression) error {
f.SaveRegister(f.CPU.Input[0])
f.SaveRegister(f.CPU.Input[1])
f.RegisterRegister(asm.MOVE, f.CPU.Input[0], variable.Register)
f.RegisterNumber(asm.MOVE, f.CPU.Input[1], int(variable.Type.(*types.Pointer).To.Size()))
f.RegisterNumber(asm.MOVE, f.CPU.Input[1], variable.Type.(*types.Pointer).To.Size())
f.CallSafe(f.Functions["mem.free"], f.CPU.Input[:2])
return nil
}

View File

@ -13,30 +13,28 @@ import (
func (f *Function) ToNumber(t token.Token) (int, error) {
switch t.Kind {
case token.Number:
digits := t.Text(f.File.Bytes)
var (
digits = t.Text(f.File.Bytes)
number int64
err error
)
if strings.HasPrefix(digits, "0x") {
number, err := strconv.ParseInt(digits[2:], 16, 64)
return int(number), err
switch {
case strings.HasPrefix(digits, "0x"):
number, err = strconv.ParseInt(digits[2:], 16, 64)
case strings.HasPrefix(digits, "0o"):
number, err = strconv.ParseInt(digits[2:], 8, 64)
case strings.HasPrefix(digits, "0b"):
number, err = strconv.ParseInt(digits[2:], 2, 64)
default:
number, err = strconv.ParseInt(digits, 10, 64)
}
if strings.HasPrefix(digits, "0o") {
number, err := strconv.ParseInt(digits[2:], 8, 64)
return int(number), err
}
if strings.HasPrefix(digits, "0b") {
number, err := strconv.ParseInt(digits[2:], 2, 64)
return int(number), err
}
number, err := strconv.Atoi(digits)
if err != nil {
return 0, errors.New(err, f.File, t.Position)
return 0, errors.New(errors.InvalidNumber, f.File, t.Position)
}
return number, nil
return int(number), nil
case token.Rune:
r := t.Bytes(f.File.Bytes)