add type aliases, rework compiler, remove optimization

This commit is contained in:
Neemek 2026-07-12 11:34:17 +02:00
parent 94b12f28ab
commit d54249cffe
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
12 changed files with 869 additions and 1597 deletions

View file

@ -99,8 +99,8 @@ const (
// InstructionStringConversion Take the top value on the stack and convert it to a string
InstructionStringConversion
// InstructionStringConcatenation Add two strings together, with the second value on the stack as left and the top as right
InstructionStringConcatenation
// InstructionConcatStrings Add two strings together, with the second value on the stack as left and the top as right
InstructionConcatStrings
// InstructionSwap swap the two top items on the stack (1, 2 -> 2, 1)
InstructionSwap
@ -121,13 +121,11 @@ const (
// InstructionNil Push a nil literal to the stack
InstructionNil
// InstructionNewList Push a new (empty) list to the stack
InstructionNewList
// InstructionAppend Append to a list. stack: (... > list > item) => (... > list)
InstructionAppend
// InstructionFormList Form items on the stack into a list. The 2 bytes after the instructions are the amount of
// items to include minus one. (value of 0 => 1 item, value of 1 => 2 items, etc.) The order is reversed compared
// to on the stack; the top value on the stack is the last in the list.
// items to include) The order is reversed compared to on the stack; the top value on the stack is the last in the
// list.
InstructionFormList
// InstructionConcatLists concatenate lists, producing a new list with the values of both lists. Pops two lists.
InstructionConcatLists
@ -136,6 +134,13 @@ const (
// on the stack is the last value in the tuple.
InstructionFormTuple
// InstructionIndexList index into a list. The lower item is the container, and the top item
// is the index. [..., container, index] -> [..., item]
InstructionIndexList
// InstructionIndexTuple index into a tuple. The lower item is the container, and the top item
// is the index. [..., container, index] -> [..., item]
InstructionIndexTuple
// InstructionBreakpoint for debugging purposes
InstructionBreakpoint
)
@ -220,7 +225,7 @@ func (b Bytecode) String() string {
return "ASCEND"
case InstructionStringConversion:
return "STRING_CONVERSION"
case InstructionStringConcatenation:
case InstructionConcatStrings:
return "STRING_CONCATENATION"
case InstructionSwap:
return "SWAP"
@ -232,8 +237,6 @@ func (b Bytecode) String() string {
return "FORM_LIST"
case InstructionBreakpoint:
return "BREAKPOINT"
case InstructionNewList:
return "NEW_LIST"
case InstructionAppend:
return "APPEND"
case InstructionAccessProperty:
@ -242,6 +245,12 @@ func (b Bytecode) String() string {
return "CONCAT_LISTS"
case InstructionDuplicate:
return "DUPLICATE"
case InstructionFormTuple:
return "FORM_TUPLE"
case InstructionIndexList:
return "INDEX_LIST"
case InstructionIndexTuple:
return "INDEX_TUPLE"
}
return "UNDEFINED"
}
@ -251,7 +260,7 @@ type Chunk struct {
Constants []Value
}
func (c Chunk) String() string {
func (c *Chunk) String() string {
b := strings.Builder{}
b.WriteString("=v= chunk =v=\n")
@ -324,7 +333,7 @@ func RegisterGOBTypes() {
}
func (c Chunk) Serialize() []byte {
func (c *Chunk) Serialize() []byte {
b := bytes.Buffer{}
e := gob.NewEncoder(&b)
@ -622,8 +631,8 @@ var DefaultGlobals = map[string]Value{
nil,
true,
},
"type": &BuiltinFunctionValue{
Name: "type",
"typeof": &BuiltinFunctionValue{
Name: "typeof",
Signature: &FunctionSignature{
In: []TypeSignature{&AnySignature{}},
Out: &StringSignature{},
@ -979,9 +988,6 @@ func (vm *VM) Next() bool {
items,
})
case InstructionNewList:
vm.Stack.Push(&ListValue{[]Value{}})
case InstructionAppend:
value := vm.Stack.Pop()
list := vm.Stack.Pop().(*ListValue)
@ -1018,7 +1024,7 @@ func (vm *VM) Next() bool {
v := vm.Stack.Pop()
vm.Stack.Push(&StringValue{v.String()})
case InstructionStringConcatenation:
case InstructionConcatStrings:
r := vm.Stack.Pop().(*StringValue).Text
l := vm.Stack.Pop().(*StringValue).Text
@ -1051,6 +1057,30 @@ func (vm *VM) Next() bool {
vm.Stack.Push(member)
case InstructionIndexList:
i := vm.Stack.Pop().(*IntegerValue)
l := vm.Stack.Pop().(*ListValue)
n := int(i.Number.Int64())
if 0 < n || n >= 10 {
vm.error(fmt.Sprintf("index %d out of bounds", n))
}
vm.Stack.Push(l.Items[n].Clone())
case InstructionIndexTuple:
i := vm.Stack.Pop().(*IntegerValue)
l := vm.Stack.Pop().(*TupleValue)
n := int(i.Number.Int64())
if 0 < n || n >= len(l.Items) {
vm.error(fmt.Sprintf("index %d out of bounds", n))
}
vm.Stack.Push(l.Items[n].Clone())
case InstructionBreakpoint:
/*
// I'm keeping this