This commit is contained in:
silva guimaraes 2025-08-23 20:02:18 -03:00
parent 0cd6040a1f
commit c1f75bb980
6 changed files with 140 additions and 67 deletions

75
main.go
View file

@ -28,18 +28,27 @@ func fullStep(e Expression) (Expression, error) {
func builtinFunctions(expr Expression) {
functions := map[Id]BuiltInFunction{
// "quote": {
// t: &FunctionType{
// ret: &PrimInteger,
// params: ,
// },
// f: func(e []Expression) (Expression, error) {
// if len(e) == 1 {
// return &Quoted{e[0]}, nil
// }
// return &Quoted{&List{e}}, nil
// },
// },
"+.": {
t: &FunctionType{
ret: &PrimFloat,
params: []Type{&PrimFloat, &PrimFloat},
},
f: func(args []Expression) (Expression, error) {
var sum FloatValue = 0
for _, arg := range args {
if !isValue(arg) {
panic("not a value")
}
switch x := arg.(type) {
case *FloatValue:
sum += *x
default:
panic("type error")
}
}
return &sum, nil
},
},
"+": {
t: &FunctionType{
ret: &PrimInteger,
@ -61,27 +70,27 @@ func builtinFunctions(expr Expression) {
return &sum, nil
},
},
// "-": func(args []Expression) (Expression, error) {
// v0, ok := args[0].(*IntValue)
// if !ok {
// return nil, fmt.Errorf("integer value was expected")
// }
// v1, ok := args[1].(*IntValue)
// if !ok {
// return nil, fmt.Errorf("integer value was expected")
// }
// var result IntValue = (*v0 - *v1)
// return &result, nil
// },
// "=": func(args []Expression) (Expression, error) {
// if reflect.TypeOf(args[0]) != reflect.TypeOf(args[1]) {
// return nil, fmt.Errorf("unmatched types")
// }
// v0 := reflect.ValueOf(args[0])
// v1 := reflect.ValueOf(args[1])
// var result BoolValue = BoolValue(v0.Equal(v1))
// return &result, nil
// },
"-": {
t: &FunctionType{
ret: &PrimInteger,
params: []Type{&PrimInteger, &PrimInteger},
},
f: func(args []Expression) (Expression, error) {
var sum IntValue = 0
for _, arg := range args {
if !isValue(arg) {
panic("not a value")
}
switch x := arg.(type) {
case *IntValue:
sum += *x
default:
panic("type error")
}
}
return &sum, nil
},
},
}
for id, f := range functions {
expr.Substitute(id, &f)