Fixed incorrect const values
This commit is contained in:
@ -8,6 +8,6 @@ import (
|
||||
// Constant registers a single value to be accessible under a descriptive name.
|
||||
type Constant struct {
|
||||
Name string
|
||||
Value token.Token
|
||||
Token token.Token
|
||||
File *fs.File
|
||||
}
|
||||
|
@ -64,14 +64,16 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
|
||||
f.FreeRegister(register)
|
||||
}
|
||||
|
||||
_, isArray := typ.(*types.Array)
|
||||
arrayType, isArray := typ.(*types.Array)
|
||||
|
||||
if isArray {
|
||||
typ = types.AnyPointer
|
||||
typ = &types.Pointer{To: arrayType.Of}
|
||||
} else if right.Token.Kind == token.Identifier {
|
||||
rightVariable := f.VariableByName(right.Token.Text(f.File.Bytes))
|
||||
leftPointer, leftIsPointer := typ.(*types.Pointer)
|
||||
rightPointer, rightIsPointer := rightVariable.Type.(*types.Pointer)
|
||||
|
||||
if typ == types.AnyPointer && rightVariable.Type == types.AnyPointer {
|
||||
if leftIsPointer && rightIsPointer && leftPointer.To == rightPointer.To {
|
||||
typ = types.Int
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ func (f *Function) Fold(expr *expression.Expression) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
value, err := f.ToNumber(constant.Value)
|
||||
value, err := ToNumber(constant.Token, constant.File)
|
||||
expr.Value = value
|
||||
expr.IsFolded = true
|
||||
return err
|
||||
|
@ -36,7 +36,15 @@ func (f *Function) PeriodToRegister(node *expression.Expression, register cpu.Re
|
||||
constant, isConst := f.Constants[f.Package+"."+leftText+"."+rightText]
|
||||
|
||||
if isConst {
|
||||
return f.TokenToRegister(constant.Value, register)
|
||||
number, err := ToNumber(constant.Token, constant.File)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f.SaveRegister(register)
|
||||
f.RegisterNumber(asm.MOVE, register, number)
|
||||
return types.Int, nil
|
||||
}
|
||||
|
||||
uniqueName := fmt.Sprintf("%s.%s", leftText, rightText)
|
||||
|
@ -6,15 +6,21 @@ import (
|
||||
"unicode/utf8"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/errors"
|
||||
"git.akyoto.dev/cli/q/src/fs"
|
||||
"git.akyoto.dev/cli/q/src/token"
|
||||
)
|
||||
|
||||
// ToNumber tries to convert the token into a numeric value.
|
||||
func (f *Function) ToNumber(t token.Token) (int, error) {
|
||||
return ToNumber(t, f.File)
|
||||
}
|
||||
|
||||
// ToNumber tries to convert the token into a numeric value.
|
||||
func ToNumber(t token.Token, file *fs.File) (int, error) {
|
||||
switch t.Kind {
|
||||
case token.Number:
|
||||
var (
|
||||
digits = t.Text(f.File.Bytes)
|
||||
digits = t.Text(file.Bytes)
|
||||
number int64
|
||||
err error
|
||||
)
|
||||
@ -31,27 +37,27 @@ func (f *Function) ToNumber(t token.Token) (int, error) {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return 0, errors.New(errors.InvalidNumber, f.File, t.Position)
|
||||
return 0, errors.New(errors.InvalidNumber, file, t.Position)
|
||||
}
|
||||
|
||||
return int(number), nil
|
||||
|
||||
case token.Rune:
|
||||
r := t.Bytes(f.File.Bytes)
|
||||
r := t.Bytes(file.Bytes)
|
||||
r = String(r)
|
||||
|
||||
if len(r) == 0 {
|
||||
return 0, errors.New(errors.InvalidRune, f.File, t.Position+1)
|
||||
return 0, errors.New(errors.InvalidRune, file, t.Position+1)
|
||||
}
|
||||
|
||||
number, size := utf8.DecodeRune(r)
|
||||
|
||||
if len(r) > size {
|
||||
return 0, errors.New(errors.InvalidRune, f.File, t.Position+1)
|
||||
return 0, errors.New(errors.InvalidRune, file, t.Position+1)
|
||||
}
|
||||
|
||||
return int(number), nil
|
||||
}
|
||||
|
||||
return 0, errors.New(errors.InvalidNumber, f.File, t.Position)
|
||||
return 0, errors.New(errors.InvalidNumber, file, t.Position)
|
||||
}
|
||||
|
@ -29,6 +29,10 @@ func (s *Scanner) queueDirectory(directory string, pkg string) {
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasSuffix(name, "_unix.q") && config.TargetOS != config.Linux && config.TargetOS != config.Mac {
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasSuffix(name, "_windows.q") && config.TargetOS != config.Windows {
|
||||
return
|
||||
}
|
||||
|
@ -25,18 +25,18 @@ func (s *Scanner) scanConst(file *fs.File, tokens token.List, i int) (int, error
|
||||
i++
|
||||
|
||||
for i < len(tokens) {
|
||||
if tokens[i].Kind == token.Identifier {
|
||||
switch tokens[i].Kind {
|
||||
case token.Identifier:
|
||||
name := tokens[i].Text(file.Bytes)
|
||||
i++
|
||||
|
||||
s.constants <- &core.Constant{
|
||||
Name: file.Package + "." + groupName + "." + name,
|
||||
Value: tokens[i],
|
||||
Token: tokens[i],
|
||||
File: file,
|
||||
}
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.BlockEnd {
|
||||
case token.BlockEnd:
|
||||
return i, nil
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,8 @@ func (s *Scanner) scanExtern(file *fs.File, tokens token.List, i int) (int, erro
|
||||
i++
|
||||
|
||||
for i < len(tokens) {
|
||||
if tokens[i].Kind == token.Identifier {
|
||||
switch tokens[i].Kind {
|
||||
case token.Identifier:
|
||||
function, j, err := scanFunctionSignature(file, tokens, i, token.NewLine)
|
||||
|
||||
if err != nil {
|
||||
@ -35,9 +36,8 @@ func (s *Scanner) scanExtern(file *fs.File, tokens token.List, i int) (int, erro
|
||||
function.Package = dllName
|
||||
function.UniqueName = dllName + "." + function.Name
|
||||
s.functions <- function
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.BlockEnd {
|
||||
case token.BlockEnd:
|
||||
return i, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user