707 lines
16 KiB
Go
707 lines
16 KiB
Go
package analyzer
|
|
|
|
import "fmt"
|
|
import "github.com/alecthomas/participle/v2"
|
|
import "git.tebibyte.media/sashakoshka/fspl/entity"
|
|
|
|
// All expression analysis routines must take in the type they are being
|
|
// assigned to and return an error if they can't be assigned to it. if the type
|
|
// is nil, the expression must ignore it unless it can do upwards type
|
|
// inference.
|
|
|
|
func (this *Tree) analyzeVariable (
|
|
into entity.Type,
|
|
mode strictness,
|
|
variable *entity.Variable,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
declaration := this.variable(variable.Name)
|
|
if declaration == nil {
|
|
return nil, participle.Errorf (
|
|
variable.Pos, "no variable named %s",
|
|
variable.Name)
|
|
}
|
|
|
|
err := this.canAssign(variable.Pos, into, mode, declaration.Type())
|
|
if err != nil { return nil, err }
|
|
|
|
variable.Declaration = declaration
|
|
return variable, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeDeclaration (
|
|
into entity.Type,
|
|
mode strictness,
|
|
declaration *entity.Declaration,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
scope, _ := this.topScope()
|
|
existing := scope.Variable(declaration.Name)
|
|
if existing != nil {
|
|
return nil, participle.Errorf (
|
|
declaration.Pos,
|
|
"%s already declared in block at %v",
|
|
declaration.Name, existing.Pos)
|
|
}
|
|
|
|
ty, err := this.analyzeType(declaration.Ty, false)
|
|
declaration.Ty = ty
|
|
if err != nil { return nil, err }
|
|
|
|
err = this.canAssign(declaration.Pos, into, mode, declaration.Type())
|
|
if err != nil { return nil, err }
|
|
|
|
this.addVariable(declaration)
|
|
return declaration, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeCall (
|
|
into entity.Type,
|
|
mode strictness,
|
|
call *entity.Call,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
// get function
|
|
function, err := this.analyzeFunction(call.Pos, call.Name)
|
|
call.Function = function
|
|
if err != nil { return nil, err }
|
|
|
|
// check return result
|
|
err = this.canAssign(call.Pos, into, mode, function.Signature.Return)
|
|
if err != nil { return nil, err }
|
|
|
|
// check arg count
|
|
if len(call.Arguments) > len(function.Signature.Arguments) {
|
|
return nil, participle.Errorf (
|
|
call.Pos, "too many arguments in call to %s",
|
|
call.Name)
|
|
} else if len(call.Arguments) < len(function.Signature.Arguments) {
|
|
return nil, participle.Errorf (
|
|
call.Pos, "too few arguments in call to %s",
|
|
call.Name)
|
|
}
|
|
|
|
// check arg types
|
|
for index, argument := range call.Arguments {
|
|
signature := function.Signature
|
|
correct := signature.ArgumentMap[signature.ArgumentOrder[index]]
|
|
argument, err := this.analyzeExpression(correct.Type(), strict, argument)
|
|
if err != nil { return nil, err }
|
|
call.Arguments[index] = argument
|
|
}
|
|
|
|
return call, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeMethodCall (
|
|
into entity.Type,
|
|
mode strictness,
|
|
call *entity.MethodCall,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
// get method
|
|
sourceExpr, err := this.analyzeVariable(nil, strict, call.Source)
|
|
source := sourceExpr.(*entity.Variable)
|
|
if err != nil { return nil, err }
|
|
method, err := this.analyzeMethodOrBehavior (
|
|
call.Pos, source.Declaration.Type(), call.Name)
|
|
if err != nil { return nil, err }
|
|
|
|
// extract signature
|
|
var signature *entity.Signature
|
|
switch method.(type) {
|
|
case *entity.Signature:
|
|
signature = method.(*entity.Signature)
|
|
call.Behavior = signature
|
|
case *entity.Method:
|
|
signature = method.(*entity.Method).Signature
|
|
call.Method = method.(*entity.Method)
|
|
default:
|
|
panic(fmt.Sprint (
|
|
"Tree.analyzeMethodOrBehavior returned ",
|
|
method))
|
|
}
|
|
|
|
// check return result
|
|
err = this.canAssign(call.Pos, into, mode, signature.Return)
|
|
if err != nil { return nil, err }
|
|
|
|
// check arg count
|
|
if len(call.Arguments) > len(signature.Arguments) {
|
|
return nil, participle.Errorf (
|
|
call.Pos, "too many arguments in call to %s",
|
|
call.Name)
|
|
} else if len(call.Arguments) < len(signature.Arguments) {
|
|
return nil, participle.Errorf (
|
|
call.Pos, "too few arguments in call to %s",
|
|
call.Name)
|
|
}
|
|
|
|
// check arg types
|
|
for index, argument := range call.Arguments {
|
|
correct := signature.ArgumentMap[signature.ArgumentOrder[index]]
|
|
argument, err := this.analyzeExpression(correct.Type(), strict, argument)
|
|
if err != nil { return nil, err }
|
|
call.Arguments[index] = argument
|
|
}
|
|
|
|
return call, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeSubscript (
|
|
into entity.Type,
|
|
mode strictness,
|
|
subscript *entity.Subscript,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
slice, err := this.analyzeExpression (
|
|
&entity.TypeSlice {
|
|
Pos: subscript.Pos,
|
|
Element: into,
|
|
}, weak,
|
|
subscript.Slice)
|
|
if err != nil { return nil, err }
|
|
subscript.Slice = slice
|
|
subscript.Ty = into
|
|
|
|
offset, err := this.analyzeExpression (
|
|
builtinType("Index"), weak,
|
|
subscript.Offset)
|
|
if err != nil { return nil, err }
|
|
subscript.Offset = offset
|
|
|
|
var frailType bool
|
|
switch into := into.(type) {
|
|
case nil: frailType = true
|
|
case *entity.TypeSlice: frailType = into.Element == nil
|
|
}
|
|
|
|
if frailType {
|
|
ty := ReduceToBase(subscript.Slice.Type())
|
|
switch ty := ty.(type) {
|
|
case *entity.TypeSlice: subscript.Ty = ty.Element
|
|
case *entity.TypeArray: subscript.Ty = ty.Element
|
|
}
|
|
}
|
|
|
|
return subscript, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeSlice (
|
|
into entity.Type,
|
|
mode strictness,
|
|
slice *entity.Slice,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
value, err := this.analyzeExpression(into, weak, slice.Slice)
|
|
if err != nil { return nil, err }
|
|
slice.Slice = value
|
|
|
|
if slice.Start != nil {
|
|
start, err := this.analyzeExpression (
|
|
builtinType("Index"), weak,
|
|
slice.Start)
|
|
if err != nil { return nil, err }
|
|
slice.Start = start
|
|
}
|
|
|
|
if slice.End != nil {
|
|
end, err := this.analyzeExpression (
|
|
builtinType("Index"), weak,
|
|
slice.End)
|
|
if err != nil { return nil, err }
|
|
slice.End = end
|
|
}
|
|
|
|
return slice, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeLength (
|
|
into entity.Type,
|
|
mode strictness,
|
|
length *entity.Length,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
value, err := this.analyzeExpression(nil, strict, length.Slice)
|
|
if err != nil { return nil, err }
|
|
length.Slice = value
|
|
length.Ty = builtinType("Index")
|
|
|
|
return length, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeDereference (
|
|
into entity.Type,
|
|
mode strictness,
|
|
dereference *entity.Dereference,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
pointer, err := this.analyzeExpression (
|
|
&entity.TypePointer {
|
|
Pos: dereference.Pos,
|
|
Referenced: into,
|
|
}, weak,
|
|
dereference.Pointer)
|
|
if err != nil { return nil, err }
|
|
dereference.Pointer = pointer
|
|
dereference.Ty = into
|
|
|
|
var frailType bool
|
|
switch into := into.(type) {
|
|
case nil: frailType = true
|
|
case *entity.TypePointer: frailType = into.Referenced == nil
|
|
}
|
|
|
|
if frailType {
|
|
ty := ReduceToBase(dereference.Pointer.Type())
|
|
switch ty := ty.(type) {
|
|
case *entity.TypePointer: dereference.Ty = ty.Referenced
|
|
}
|
|
}
|
|
|
|
return dereference, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeReference (
|
|
into entity.Type,
|
|
mode strictness,
|
|
reference *entity.Reference,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
referenced, ok := into.(*entity.TypePointer)
|
|
if !ok {
|
|
return nil, participle.Errorf(reference.Pos, "expected %v", into)
|
|
}
|
|
|
|
value, err := this.analyzeExpression (
|
|
referenced.Referenced, weak,
|
|
reference.Value)
|
|
if err != nil { return nil, err }
|
|
err = this.isLocationExpression(reference.Value)
|
|
if err != nil { return nil, err }
|
|
reference.Value = value
|
|
reference.Ty = referenced.Referenced
|
|
|
|
return reference, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeValueCast (
|
|
into entity.Type,
|
|
mode strictness,
|
|
cast *entity.ValueCast,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
ty, err := this.analyzeType(cast.Ty, false)
|
|
if err != nil { return nil, err }
|
|
cast.Ty = ty
|
|
|
|
err = this.canAssign(cast.Pos, into, mode, cast.Type())
|
|
if err != nil { return nil, err }
|
|
|
|
value, err := this.analyzeExpression(cast.Ty, coerce, cast.Value)
|
|
if err != nil { return nil, err }
|
|
cast.Value = value
|
|
|
|
return cast, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeBitCast (
|
|
into entity.Type,
|
|
mode strictness,
|
|
cast *entity.BitCast,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
ty, err := this.analyzeType(cast.Ty, false)
|
|
if err != nil { return nil, err }
|
|
cast.Ty = ty
|
|
|
|
err = this.canAssign(cast.Pos, into, mode, cast.Type())
|
|
if err != nil { return nil, err }
|
|
|
|
value, err := this.analyzeExpression(cast.Ty, force, cast.Value)
|
|
if err != nil { return nil, err }
|
|
cast.Value = value
|
|
|
|
return cast, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeOperation (
|
|
into entity.Type,
|
|
mode strictness,
|
|
operation *entity.Operation,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
wrongInto := func () (entity.Expression, error) {
|
|
return nil, participle.Errorf(operation.Pos, "expected %v", into)
|
|
}
|
|
|
|
wrongArgCount := func () (entity.Expression, error) {
|
|
return nil, participle.Errorf (
|
|
operation.Pos, "wrong argument count for %v",
|
|
operation.Operator)
|
|
}
|
|
|
|
nSameType := func (n int, constraint func (entity.Type) bool) (entity.Expression, error) {
|
|
if n > 0 {
|
|
if len(operation.Arguments) != n { return wrongArgCount() }
|
|
} else {
|
|
if len(operation.Arguments) < 1 { return wrongArgCount() }
|
|
}
|
|
n = len(operation.Arguments)
|
|
if !constraint(into) { return wrongInto() }
|
|
|
|
left, err := this.analyzeExpression(into, mode, operation.Arguments[0])
|
|
if err != nil { return nil, err }
|
|
operation.Arguments[0] = left
|
|
operation.Ty = left.Type()
|
|
|
|
for index := 1; index < n; index ++ {
|
|
argument, err := this.analyzeExpression (
|
|
left.Type(), strict,
|
|
operation.Arguments[index])
|
|
if err != nil { return nil, err }
|
|
operation.Arguments[index] = argument
|
|
}
|
|
|
|
return operation, nil
|
|
}
|
|
|
|
comparison := func (argConstraint func (entity.Type) bool) (entity.Expression, error) {
|
|
if len(operation.Arguments) < 2 { return wrongArgCount() }
|
|
if !isBoolean(into) { return wrongInto() }
|
|
operation.Ty = &entity.TypeNamed {
|
|
Name: "Bool",
|
|
Type: this.Types["Bool"].Type,
|
|
}
|
|
|
|
// find the first argument that has explicit type information.
|
|
// TODO: possibly make a method of expressions to check this
|
|
// without analyzing anything
|
|
boss := -1
|
|
var argumentType entity.Type
|
|
for index, argument := range operation.Arguments {
|
|
argument, err := this.analyzeExpression(nil, strict, argument)
|
|
if err == nil {
|
|
boss = index
|
|
operation.Arguments[index] = argument
|
|
argumentType = argument.Type()
|
|
break
|
|
}
|
|
}
|
|
if argumentType == nil {
|
|
return nil, participle.Errorf (
|
|
operation.Pos,
|
|
"operation arguments have ambiguous type")
|
|
}
|
|
|
|
// analyze all remaining arguments
|
|
for index, argument := range operation.Arguments {
|
|
if index == boss { continue }
|
|
|
|
argument, err := this.analyzeExpression (
|
|
argumentType, strict, argument)
|
|
if err != nil {
|
|
boss = index
|
|
operation.Arguments[index] = argument
|
|
break
|
|
}
|
|
operation.Arguments[index] = argument
|
|
}
|
|
|
|
return operation, nil
|
|
}
|
|
|
|
switch operation.Operator {
|
|
// math
|
|
case entity.OperatorAdd,
|
|
entity.OperatorSubtract,
|
|
entity.OperatorMultiply,
|
|
entity.OperatorDivide,
|
|
entity.OperatorIncrement,
|
|
entity.OperatorDecrement:
|
|
return nSameType(-1, isNumeric)
|
|
|
|
case entity.OperatorModulo:
|
|
return nSameType(2, isNumeric)
|
|
|
|
// logic
|
|
case entity.OperatorLogicalNot:
|
|
return nSameType(1, isBoolean)
|
|
|
|
case entity.OperatorLogicalOr,
|
|
entity.OperatorLogicalAnd,
|
|
entity.OperatorLogicalXor:
|
|
return nSameType(-1, isBoolean)
|
|
|
|
// bit manipulation
|
|
case entity.OperatorNot:
|
|
return nSameType(1, isInteger)
|
|
|
|
case entity.OperatorOr, entity.OperatorAnd, entity.OperatorXor:
|
|
return nSameType(-1, isInteger)
|
|
|
|
case entity.OperatorLeftShift, entity.OperatorRightShift:
|
|
if len(operation.Arguments) != 2 { return wrongArgCount() }
|
|
if !isInteger(into) { return wrongInto() }
|
|
|
|
arg, err := this.analyzeExpression(into, mode, operation.Arguments[0])
|
|
if err != nil { return nil, err }
|
|
operation.Arguments[0] = arg
|
|
operation.Ty = arg.Type()
|
|
|
|
offset, err := this.analyzeExpression (
|
|
builtinType("Index"), weak,
|
|
operation.Arguments[1])
|
|
if err != nil { return nil, err }
|
|
operation.Arguments[1] = offset
|
|
|
|
return operation, nil
|
|
|
|
// comparison
|
|
case entity.OperatorLess,
|
|
entity.OperatorGreater,
|
|
entity.OperatorLessEqual,
|
|
entity.OperatorGreaterEqual:
|
|
return comparison(isOrdered)
|
|
|
|
case entity.OperatorEqual:
|
|
return comparison(isOrdered)
|
|
|
|
default:
|
|
panic(fmt.Sprint (
|
|
"BUG: analyzer doesnt know about operator ",
|
|
operation.Operator))
|
|
}
|
|
}
|
|
|
|
func (this *Tree) analyzeBlock (
|
|
into entity.Type,
|
|
mode strictness,
|
|
block *entity.Block,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
this.pushScope(block)
|
|
defer this.popScope()
|
|
|
|
if len(block.Steps) == 0 && into != nil {
|
|
return nil, participle.Errorf (
|
|
block.Pos, "block must have at least one statement")
|
|
}
|
|
|
|
final := len(block.Steps) - 1
|
|
for index, step := range block.Steps {
|
|
if index == final && into != nil {
|
|
expression, ok := step.(entity.Expression)
|
|
if !ok {
|
|
return nil, participle.Errorf (
|
|
block.Pos, "expected expression")
|
|
}
|
|
|
|
step, err := this.analyzeExpression(into, strict, expression)
|
|
if err != nil { return nil, err }
|
|
block.Steps[index] = step
|
|
block.Ty = step.Type()
|
|
} else {
|
|
step, err := this.analyzeStatement(step)
|
|
if err != nil { return nil, err }
|
|
block.Steps[index] = step
|
|
}
|
|
}
|
|
|
|
return block, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeMemberAccess (
|
|
into entity.Type,
|
|
mode strictness,
|
|
access *entity.MemberAccess,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
source, err := this.analyzeExpression(nil, strict, access.Source)
|
|
if err != nil { return nil, err }
|
|
access.Source = source.(*entity.Variable)
|
|
|
|
var sourceType *entity.TypeStruct
|
|
switch sourceTypeAny := ReduceToBase(source.Type()).(type) {
|
|
case *entity.TypeStruct: sourceType = sourceTypeAny
|
|
case *entity.TypePointer:
|
|
referenced, ok := ReduceToBase(sourceTypeAny.Referenced).(*entity.TypeStruct)
|
|
if !ok {
|
|
return nil, participle.Errorf (
|
|
access.Pos, "cannot access members of %v", source)
|
|
}
|
|
sourceType = referenced
|
|
default:
|
|
return nil, participle.Errorf (
|
|
access.Pos, "cannot access members of %v", source)
|
|
}
|
|
|
|
member, ok := sourceType.MemberMap[access.Member]
|
|
if !ok {
|
|
return nil, participle.Errorf (
|
|
access.Pos, "no member %v", access)
|
|
}
|
|
|
|
err = this.canAssign(access.Pos, into, mode, member.Type())
|
|
if err != nil { return nil, err }
|
|
access.Ty = member.Type()
|
|
|
|
return access, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeIfElse (
|
|
into entity.Type,
|
|
mode strictness,
|
|
ifelse *entity.IfElse,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
condition, err := this.analyzeExpression (
|
|
&entity.TypeNamed {
|
|
Name: "Bool",
|
|
Type: this.Types["Bool"].Type,
|
|
},
|
|
weak, ifelse.Condition)
|
|
if err != nil { return nil, err }
|
|
ifelse.Condition = condition
|
|
|
|
trueBranch, err := this.analyzeExpression(into, strict, ifelse.True)
|
|
if err != nil { return nil, err }
|
|
ifelse.True = trueBranch
|
|
|
|
if ifelse.False == nil {
|
|
if into != nil {
|
|
return nil, participle.Errorf (
|
|
ifelse.Pos,
|
|
"else case required when using value of if ")
|
|
}
|
|
} else {
|
|
falseBranch, err := this.analyzeExpression(into, strict, ifelse.False)
|
|
if err != nil { return nil, err }
|
|
ifelse.False = falseBranch
|
|
}
|
|
|
|
ifelse.Ty = into
|
|
|
|
return ifelse, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeLoop (
|
|
into entity.Type,
|
|
mode strictness,
|
|
loop *entity.Loop,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
loop.Ty = into
|
|
this.pushLoop(loop)
|
|
defer this.popLoop()
|
|
|
|
body, err := this.analyzeExpression(nil, strict, loop.Body)
|
|
if err != nil { return nil, err }
|
|
loop.Body = body
|
|
|
|
return loop, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeBreak (
|
|
into entity.Type,
|
|
mode strictness,
|
|
brk *entity.Break,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
if into != nil {
|
|
return nil, participle.Errorf(brk.Pos, "expected %v", into)
|
|
}
|
|
|
|
loop, ok := this.topLoop()
|
|
if !ok {
|
|
return nil, participle.Errorf (
|
|
brk.Pos,
|
|
"break statement must be within loop")
|
|
}
|
|
brk.Loop = loop
|
|
|
|
if loop.Type() != nil && brk.Value == nil {
|
|
return nil, participle.Errorf (
|
|
brk.Pos,
|
|
"break statement must have value")
|
|
}
|
|
|
|
if brk.Value != nil {
|
|
value, err := this.analyzeExpression(loop.Type(), strict, brk.Value)
|
|
if err != nil { return nil, err }
|
|
brk.Value = value
|
|
}
|
|
|
|
return brk, nil
|
|
}
|
|
|
|
func (this *Tree) analyzeReturn (
|
|
into entity.Type,
|
|
mode strictness,
|
|
ret *entity.Return,
|
|
) (
|
|
entity.Expression,
|
|
error,
|
|
) {
|
|
if into != nil {
|
|
return nil, participle.Errorf(ret.Pos, "expected %v", into)
|
|
}
|
|
|
|
ret.Declaration, _ = this.topDeclaration()
|
|
var ty entity.Type
|
|
switch ret.Declaration.(type) {
|
|
case *entity.Function:
|
|
ty = ret.Declaration.(*entity.Function).Signature.Return
|
|
case *entity.Method:
|
|
ty = ret.Declaration.(*entity.Method).Signature.Return
|
|
}
|
|
|
|
if ty != nil && ret.Value == nil {
|
|
return nil, participle.Errorf (
|
|
ret.Pos,
|
|
"break statement must have value")
|
|
}
|
|
|
|
if ret.Value != nil {
|
|
value, err := this.analyzeExpression(ty, strict, ret.Value)
|
|
if err != nil { return nil, err }
|
|
ret.Value = value
|
|
}
|
|
|
|
return ret, nil
|
|
}
|