diff --git a/lib/pinc_bytecode/bytecode.ml b/lib/pinc_bytecode/bytecode.ml index 7faad81..467d93b 100644 --- a/lib/pinc_bytecode/bytecode.ml +++ b/lib/pinc_bytecode/bytecode.ml @@ -29,8 +29,8 @@ let pp_value fmt = function Format.fprintf fmt "'%s'" @@ Buffer.contents buf | Value.Array _ -> Format.fprintf fmt "" | Value.Record _ -> Format.fprintf fmt "" - | Value.Function instructions -> - Format.fprintf fmt " [@;@[ %a@]@;]" pp_instructions instructions + | Value.Function fn -> + Format.fprintf fmt " [@;@[ %a@]@;]" pp_instructions fn.instructions ;; let pp_constants fmt constants = diff --git a/lib/pinc_bytecode/instruction.ml b/lib/pinc_bytecode/instruction.ml index 1d3e727..6bdd0f7 100644 --- a/lib/pinc_bytecode/instruction.ml +++ b/lib/pinc_bytecode/instruction.ml @@ -33,6 +33,8 @@ type t = | I_Range_Inclusive | I_Call | I_Return + | I_Set_Local of Int32.t + | I_Get_Local of Int32.t let byte = function | I_Pop -> 0x00 @@ -69,6 +71,8 @@ let byte = function | I_Range_Inclusive -> 0x1F | I_Call -> 0x20 | I_Return -> 0x21 + | I_Set_Local _ -> 0x22 + | I_Get_Local _ -> 0x23 ;; let operands_length = function @@ -78,7 +82,9 @@ let operands_length = function | I_Set_Global op | I_Get_Global op | I_Array op - | I_Record op -> Int32.byte_width op + | I_Record op + | I_Set_Local op + | I_Get_Local op -> Int32.byte_width op | I_Pop | I_Add | I_Sub @@ -160,6 +166,12 @@ let decode bytes offset = | 0x1F -> (offset, I_Range_Inclusive) | 0x20 -> (offset, I_Call) | 0x21 -> (offset, I_Return) + | 0x22 -> + let offset, addr = Int32.read_bytes bytes offset in + (offset, I_Set_Local addr) + | 0x23 -> + let offset, addr = Int32.read_bytes bytes offset in + (offset, I_Get_Local addr) | _ -> raise_notrace (Invalid_argument (Printf.sprintf "unknown instruction: 0x%.2X" instruction)) @@ -200,6 +212,8 @@ let pp fmt = function | I_Range_Inclusive -> Format.fprintf fmt "I_Range_Inclusive" | I_Call -> Format.fprintf fmt "I_Call" | I_Return -> Format.fprintf fmt "I_Return" + | I_Get_Local addr -> Format.fprintf fmt "I_Get_Local %a" Int32.pp addr + | I_Set_Local addr -> Format.fprintf fmt "I_Set_Local %a" Int32.pp addr ;; let to_bytes t = @@ -220,7 +234,9 @@ let to_bytes t = | I_Set_Global op | I_Get_Global op | I_Array op - | I_Record op -> offset := Int32.write_bytes bytes !offset op + | I_Record op + | I_Set_Local op + | I_Get_Local op -> offset := Int32.write_bytes bytes !offset op | I_Pop | I_Add | I_Sub diff --git a/lib/pinc_bytecode/value.ml b/lib/pinc_bytecode/value.ml index 896cbc0..4201adb 100644 --- a/lib/pinc_bytecode/value.ml +++ b/lib/pinc_bytecode/value.ml @@ -7,7 +7,10 @@ type t = | String of string | Array of t array | Record of t StringMap.t - | Function of Bytes.t + | Function of { + locals : int; + instructions : Bytes.t; + } let rec to_string = function | Null -> "" @@ -67,7 +70,8 @@ let rec equal a b = | Null, Null -> true | Array a, Array b -> Array.equal equal a b | Record a, Record b -> StringMap.equal equal a b - | Function a, Function b -> Bytes.equal a b + | Function a, Function b -> + Int.equal a.locals b.locals && Bytes.equal a.instructions b.instructions | _ -> false ;; diff --git a/lib/pinc_compiler/compiler.ml b/lib/pinc_compiler/compiler.ml index c9b8be1..2d1e98d 100644 --- a/lib/pinc_compiler/compiler.ml +++ b/lib/pinc_compiler/compiler.ml @@ -17,6 +17,8 @@ type t = { scopes : scope list; } +let empty_instruction = { offset = 0; instruction = I_Null } + let current_scope t = match t.scopes with | [] -> assert false @@ -81,7 +83,6 @@ let match_last_instruction t check = ;; let add_scope t = - let empty_instruction = { offset = 0; instruction = I_Null } in let scope = { instructions = Buffer.create 8; @@ -89,18 +90,23 @@ let add_scope t = last_instruction = empty_instruction; } in - { t with scopes = scope :: t.scopes } + { + t with + scopes = scope :: t.scopes; + symbol_table = SymbolTable.add_scope t.symbol_table; + } ;; let pop_scope t = match t.scopes with | [] -> assert false - | scope :: scopes -> ({ t with scopes }, scope) + | scope :: scopes -> + ({ t with scopes; symbol_table = SymbolTable.pop_scope t.symbol_table }, scope) ;; let add_constant = let id = - let id' = ref Int32.zero in + let id' = ref Int32.minus_one in fun () -> id' := Int32.succ !id'; !id' @@ -137,15 +143,30 @@ let get_symbol ~loc t name = | Some symbol -> symbol ;; +let emit_get_symbol t ~loc name = + (* + REFACTOR: + This should only be needed in the indentifier. + Currently the string interpolation is also getting symbols directly. + The String interpolation should be rewritten, in the transformer so that + it becomes just plain strings with concatenated identifiers. + *) + let symbol = get_symbol t ~loc name in + let instruction = + match SymbolTable.Symbol.scope symbol with + | SymbolTable.Scope.Global -> Pinc_Bytecode.Instruction.I_Get_Global symbol.address + | SymbolTable.Scope.Local -> Pinc_Bytecode.Instruction.I_Get_Local symbol.address + in + emit t instruction +;; + let compile_string_template t s = s |> List.fold_left (fun (t, index) template -> let t = match template.Pinc_Types.Ast.string_template_desc with - | StringInterpolation (Lowercase_Id (name, loc)) -> - let symbol = get_symbol t ~loc name in - emit t @@ Pinc_Bytecode.Instruction.I_Get_Global symbol.address + | StringInterpolation (Lowercase_Id (name, loc)) -> emit_get_symbol t ~loc name | StringText s -> emit_constant t (Pinc_Bytecode.Value.String s) in let t = @@ -168,9 +189,7 @@ let rec compile_expr t (expr : Pinc_Types.Ast.expression) = | Float f -> emit_constant t (Pinc_Bytecode.Value.Float f) | Bool true -> emit t Pinc_Bytecode.Instruction.I_True | Bool false -> emit t Pinc_Bytecode.Instruction.I_False - | LowercaseIdentifierExpression name -> - let symbol = get_symbol ~loc:expr.expression_loc t name in - emit t @@ Pinc_Bytecode.Instruction.I_Get_Global symbol.address + | LowercaseIdentifierExpression name -> emit_get_symbol t ~loc:expr.expression_loc name | ExternalFunction _ -> raise_notrace TODO | UppercaseIdentifierExpression _ -> raise_notrace TODO | Array a -> @@ -209,9 +228,10 @@ let rec compile_expr t (expr : Pinc_Types.Ast.expression) = else t in + let locals = SymbolTable.length t.symbol_table in let t, scope = pop_scope t in let instructions = Buffer.to_bytes scope.instructions in - let t = emit_constant t @@ Pinc_Bytecode.Value.Function instructions in + let t = emit_constant t @@ Pinc_Bytecode.Value.Function { locals; instructions } in t | FunctionCall { function_definition; arguments = _ } -> let t = compile_expr t function_definition in @@ -372,8 +392,15 @@ and compile_stmt t (stmt : Pinc_Types.Ast.statement) = | ContinueStatement _ -> raise_notrace TODO | LetStatement (~is_optional:_, ~is_mutable:_, Lowercase_Id (name, _), expr) -> let t = compile_expr t expr in - let t, addr = add_symbol t name in - let t = emit t (Pinc_Bytecode.Instruction.I_Set_Global addr) in + let t, symbol = add_symbol t name in + let instruction = + match SymbolTable.Symbol.scope symbol with + | SymbolTable.Scope.Global -> + Pinc_Bytecode.Instruction.I_Set_Global (SymbolTable.Symbol.address symbol) + | SymbolTable.Scope.Local -> + Pinc_Bytecode.Instruction.I_Set_Local (SymbolTable.Symbol.address symbol) + in + let t = emit t instruction in t | MutationStatement (_, _) -> raise_notrace TODO | ExpressionStatement e -> @@ -396,10 +423,20 @@ let compile_declaration (decl : Pinc_Types.Ast.declaration) t = ;; let compile (ast : Pinc_Types.Ast.t) = + let scope = + { + instructions = Buffer.create 8; + previous_instruction = empty_instruction; + last_instruction = empty_instruction; + } + in let t = - { constants = Int32.Map.empty; symbol_table = SymbolTable.make (); scopes = [] } + { + constants = Int32.Map.empty; + symbol_table = SymbolTable.make (); + scopes = [ scope ]; + } in - let t = add_scope t in let t = StringMap.fold (fun _ -> compile_declaration) ast t in Pinc_Bytecode.Bytecode.make ~instructions:(Buffer.to_bytes @@ current_instructions t) diff --git a/lib/pinc_core/SymbolTable.ml b/lib/pinc_core/SymbolTable.ml index 67d7a86..e4df159 100644 --- a/lib/pinc_core/SymbolTable.ml +++ b/lib/pinc_core/SymbolTable.ml @@ -1,22 +1,63 @@ +module Scope = struct + type t = + | Global + | Local +end + +module Symbol = struct + type t = { + name : string; + scope : Scope.t; + address : Int32.t; + } + + let make ~name ~scope ~address = { name; scope; address } + let name t = t.name + let scope t = t.scope + let address t = t.address +end + type t = { - store : symbol StringMap.t; - last_used_address : Int32.t; + store : Symbol.t StringMap.t; + num_bindings : Int32.t; + outer : t option; } -and symbol = { - name : string; - scope : scope; - address : Int32.t; -} +let make () = { store = StringMap.empty; num_bindings = Int32.zero; outer = None } -and scope = Global +let add_scope t = + let t' = make () in + { t' with outer = Some t } +;; -let make () = { store = StringMap.empty; last_used_address = Int32.zero } +let pop_scope t = + match t.outer with + | None -> assert false + | Some t -> t +;; let define_symbol t ~name = - let address = Int32.succ t.last_used_address in - let symbol = { name; scope = Global; address } in - ({ store = StringMap.add name symbol t.store; last_used_address = address }, address) + let scope = + match t.outer with + | None -> Scope.Global + | Some _ -> Scope.Local + in + let symbol = Symbol.make ~name ~scope ~address:t.num_bindings in + let t' = + { + t with + store = StringMap.add name symbol t.store; + num_bindings = Int32.succ t.num_bindings; + } + in + (t', symbol) +;; + +let rec resolve_symbol t ~name = + let symbol = StringMap.find_opt name t.store in + match (symbol, t.outer) with + | None, Some outer -> resolve_symbol outer ~name + | _ -> symbol ;; -let resolve_symbol t ~name = StringMap.find_opt name t.store +let length t = Int32.to_int t.num_bindings diff --git a/lib/pinc_vm/vm.ml b/lib/pinc_vm/vm.ml index c4d4b07..d6e29f0 100644 --- a/lib/pinc_vm/vm.ml +++ b/lib/pinc_vm/vm.ml @@ -15,7 +15,7 @@ type t = { let stack_size = 2048 let make (bytecode : Bytecode.t) = - let main_frame = Frame.make bytecode.instructions in + let main_frame = Frame.make 0 bytecode.instructions in { constants = bytecode.constants; stack = Stack.make ~size:stack_size ~default_value:Value.Null; @@ -314,10 +314,12 @@ and execute_unary_not r = let run t = Printexc.record_backtrace true; - while (current_frame t).pointer < Bytes.length (current_frame t).instructions do + while + (current_frame t).instruction_pointer < Bytes.length (current_frame t).instructions + do let frame = current_frame t in - let new_ip, op = Instruction.decode frame.instructions frame.pointer in - Frame.set_pointer frame new_ip; + let new_ip, op = Instruction.decode frame.instructions frame.instruction_pointer in + Frame.set_instruction_pointer frame new_ip; let () = match op with | Instruction.I_Pop -> ignore @@ Stack.pop t.stack @@ -349,12 +351,13 @@ let run t = execute_binary_operation t Operators.Binary.INCLUSIVE_RANGE | Instruction.I_Minus -> execute_unary_operation t Operators.Unary.MINUS | Instruction.I_Not -> execute_unary_operation t Operators.Unary.NOT - | Instruction.I_Jump addr -> Frame.set_pointer frame @@ Int32.to_int addr + | Instruction.I_Jump addr -> + Frame.set_instruction_pointer frame @@ Int32.to_int addr | Instruction.I_Jump_If_False addr -> let condition = Stack.pop t.stack in let () = if not @@ Value.is_true condition then - Frame.set_pointer frame @@ Int32.to_int addr + Frame.set_instruction_pointer frame @@ Int32.to_int addr in () | Instruction.I_Null -> Stack.push t.stack Value.Null @@ -383,16 +386,28 @@ let run t = | Instruction.I_Call -> ( let fn = Stack.top t.stack in match fn with - | Value.Function instructions -> push_frame t @@ Frame.make instructions + | Value.Function fn -> + let frame = Frame.make t.stack.stack_pointer fn.instructions in + push_frame t frame; + Stack.set_pointer t.stack (frame.base_pointer + fn.locals) | _ -> raise_notrace @@ Invalid_argument "Trying to call a non function value") | Instruction.I_Return -> let value = Stack.pop t.stack in - let () = ignore @@ pop_frame t in + let frame = pop_frame t in + Stack.set_pointer t.stack frame.base_pointer; let () = (* This is the function from the I_Call instruction *) ignore @@ Stack.pop t.stack in Stack.push t.stack value + | Instruction.I_Set_Local addr -> + let value = Stack.pop t.stack in + let address = frame.base_pointer + Int32.to_int addr in + Stack.set t.stack address value + | Instruction.I_Get_Local addr -> + let address = frame.base_pointer + Int32.to_int addr in + let value = Stack.get t.stack address in + Stack.push t.stack value in () done; diff --git a/lib/pinc_vm/vm_frame.ml b/lib/pinc_vm/vm_frame.ml index da9c34f..ef4f806 100644 --- a/lib/pinc_vm/vm_frame.ml +++ b/lib/pinc_vm/vm_frame.ml @@ -1,8 +1,12 @@ type t = { + base_pointer : int; instructions : Bytes.t; - mutable pointer : int; + mutable instruction_pointer : int; } -let make instructions = { instructions; pointer = 0 } +let make base_pointer instructions = + { base_pointer; instructions; instruction_pointer = 0 } +;; + let instructions t = t.instructions -let set_pointer t i = t.pointer <- i +let set_instruction_pointer t i = t.instruction_pointer <- i diff --git a/lib/pinc_vm/vm_stack.ml b/lib/pinc_vm/vm_stack.ml index b40657a..d91e917 100644 --- a/lib/pinc_vm/vm_stack.ml +++ b/lib/pinc_vm/vm_stack.ml @@ -16,6 +16,13 @@ let make ~size ~default_value = } ;; +let set_pointer t n = + if n > t.stack_size then + raise_notrace Pinc_stack_overflow + else + t.stack_pointer <- n +;; + let push t value = if t.stack_pointer > t.stack_size then raise_notrace Pinc_stack_overflow @@ -48,4 +55,18 @@ let top t = | n -> t.stack.(n - 1) ;; +let set t address value = + if address > t.stack_size then + raise_notrace Pinc_stack_overflow + else + t.stack.(address) <- value +;; + +let get t address = + if address > t.stack_size then + raise_notrace Pinc_stack_overflow + else + t.stack.(address) +;; + let last_popped_element t = t.stack.(t.stack_pointer) diff --git a/test/vm/array.t b/test/vm/array.t index f7229b8..05795e9 100644 --- a/test/vm/array.t +++ b/test/vm/array.t @@ -11,18 +11,18 @@ $ NO_COLOR="1" print_instructions . Array [CONSTANTS] - 0x00000001 (00000001) : 1 - 0x00000002 (00000002) : 2 - 0x00000003 (00000003) : 3 - 0x00000004 (00000004) : 4 - 0x00000005 (00000005) : 5 + 0x00000000 (00000000) : 1 + 0x00000001 (00000001) : 2 + 0x00000002 (00000002) : 3 + 0x00000003 (00000003) : 4 + 0x00000004 (00000004) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) - 0010 I_Constant 0x00000003 (00000003) - 0015 I_Constant 0x00000004 (00000004) - 0020 I_Constant 0x00000005 (00000005) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) + 0010 I_Constant 0x00000002 (00000002) + 0015 I_Constant 0x00000003 (00000003) + 0020 I_Constant 0x00000004 (00000004) 0025 I_Array 5 0030 I_Pop @@ -31,27 +31,27 @@ $ NO_COLOR="1" print_instructions . ExpressionArray [CONSTANTS] - 0x00000001 (00000001) : 1 - 0x00000002 (00000002) : 2 - 0x00000003 (00000003) : 3 - 0x00000004 (00000004) : 4 - 0x00000005 (00000005) : 5 - 0x00000006 (00000006) : 6 - 0x00000007 (00000007) : 7 - 0x00000008 (00000008) : 8 + 0x00000000 (00000000) : 1 + 0x00000001 (00000001) : 2 + 0x00000002 (00000002) : 3 + 0x00000003 (00000003) : 4 + 0x00000004 (00000004) : 5 + 0x00000005 (00000005) : 6 + 0x00000006 (00000006) : 7 + 0x00000007 (00000007) : 8 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Add - 0011 I_Constant 0x00000003 (00000003) - 0016 I_Constant 0x00000004 (00000004) + 0011 I_Constant 0x00000002 (00000002) + 0016 I_Constant 0x00000003 (00000003) 0021 I_Sub - 0022 I_Constant 0x00000005 (00000005) - 0027 I_Constant 0x00000006 (00000006) + 0022 I_Constant 0x00000004 (00000004) + 0027 I_Constant 0x00000005 (00000005) 0032 I_Mul - 0033 I_Constant 0x00000007 (00000007) - 0038 I_Constant 0x00000008 (00000008) + 0033 I_Constant 0x00000006 (00000006) + 0038 I_Constant 0x00000007 (00000007) 0043 I_Div 0044 I_Array 4 0049 I_Pop @@ -61,33 +61,33 @@ $ NO_COLOR="1" print_instructions . ArrayAccess [CONSTANTS] - 0x00000001 (00000001) : 1 - 0x00000002 (00000002) : 2 - 0x00000003 (00000003) : 3 - 0x00000004 (00000004) : 4 - 0x00000005 (00000005) : 5 - 0x00000006 (00000006) : 6 - 0x00000007 (00000007) : 7 - 0x00000008 (00000008) : 8 - 0x00000009 (00000009) : 3 - 0x0000000A (00000010) : 2 + 0x00000000 (00000000) : 1 + 0x00000001 (00000001) : 2 + 0x00000002 (00000002) : 3 + 0x00000003 (00000003) : 4 + 0x00000004 (00000004) : 5 + 0x00000005 (00000005) : 6 + 0x00000006 (00000006) : 7 + 0x00000007 (00000007) : 8 + 0x00000008 (00000008) : 3 + 0x00000009 (00000009) : 2 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Add - 0011 I_Constant 0x00000003 (00000003) - 0016 I_Constant 0x00000004 (00000004) + 0011 I_Constant 0x00000002 (00000002) + 0016 I_Constant 0x00000003 (00000003) 0021 I_Sub - 0022 I_Constant 0x00000005 (00000005) - 0027 I_Constant 0x00000006 (00000006) + 0022 I_Constant 0x00000004 (00000004) + 0027 I_Constant 0x00000005 (00000005) 0032 I_Mul - 0033 I_Constant 0x00000007 (00000007) - 0038 I_Constant 0x00000008 (00000008) + 0033 I_Constant 0x00000006 (00000006) + 0038 I_Constant 0x00000007 (00000007) 0043 I_Div 0044 I_Array 4 - 0049 I_Constant 0x00000009 (00000009) - 0054 I_Constant 0x0000000A (00000010) + 0049 I_Constant 0x00000008 (00000008) + 0054 I_Constant 0x00000009 (00000009) 0059 I_Sub 0060 I_Index 0061 I_Pop @@ -97,12 +97,12 @@ $ NO_COLOR="1" print_instructions . ArrayRange [CONSTANTS] - 0x00000001 (00000001) : 1 - 0x00000002 (00000002) : 10 + 0x00000000 (00000000) : 1 + 0x00000001 (00000001) : 10 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Range 0011 I_Pop @@ -111,11 +111,11 @@ $ NO_COLOR="1" print_instructions . ArrayRangeInclusive [CONSTANTS] - 0x00000001 (00000001) : 1 - 0x00000002 (00000002) : 10 + 0x00000000 (00000000) : 1 + 0x00000001 (00000001) : 10 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Range_Inclusive 0011 I_Pop diff --git a/test/vm/bindings.t b/test/vm/bindings.t index 80c873d..436c4d3 100644 --- a/test/vm/bindings.t +++ b/test/vm/bindings.t @@ -3,14 +3,14 @@ $ NO_COLOR="1" print_instructions . Let [CONSTANTS] - 0x00000001 (00000001) : 1 + 0x00000000 (00000000) : 1 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Set_Global 0x00000001 (00000001) - 0010 I_Get_Global 0x00000001 (00000001) - 0015 I_Set_Global 0x00000002 (00000002) - 0020 I_Get_Global 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Set_Global 0x00000000 (00000000) + 0010 I_Get_Global 0x00000000 (00000000) + 0015 I_Set_Global 0x00000001 (00000001) + 0020 I_Get_Global 0x00000001 (00000001) 0025 I_Pop $ NO_COLOR="1" print_vm . Shadowing @@ -18,17 +18,17 @@ $ NO_COLOR="1" print_instructions . Shadowing [CONSTANTS] + 0x00000000 (00000000) : 1 0x00000001 (00000001) : 1 - 0x00000002 (00000002) : 1 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Set_Global 0x00000001 (00000001) - 0010 I_Get_Global 0x00000001 (00000001) - 0015 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Set_Global 0x00000000 (00000000) + 0010 I_Get_Global 0x00000000 (00000000) + 0015 I_Constant 0x00000001 (00000001) 0020 I_Add - 0021 I_Set_Global 0x00000002 (00000002) - 0026 I_Get_Global 0x00000002 (00000002) + 0021 I_Set_Global 0x00000001 (00000001) + 0026 I_Get_Global 0x00000001 (00000001) 0031 I_Pop $ NO_COLOR="1" print_instructions . UnboundIdentifier @@ -42,3 +42,24 @@ Unbound identifier `one` [1] + + + $ NO_COLOR="1" print_vm . LocalBindings + 21 + + $ NO_COLOR="1" print_instructions . LocalBindings + [CONSTANTS] + 0x00000000 (00000000) : 21 + 0x00000001 (00000001) : [ + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Set_Local 0x00000000 (00000000) + 0010 I_Get_Local 0x00000000 (00000000) + 0015 I_Return + ] + + [INSTRUCTIONS] + 0000 I_Constant 0x00000001 (00000001) + 0005 I_Set_Global 0x00000000 (00000000) + 0010 I_Get_Global 0x00000000 (00000000) + 0015 I_Call + 0016 I_Pop diff --git a/test/vm/bool.t b/test/vm/bool.t index e9f1042..7ca1d45 100644 --- a/test/vm/bool.t +++ b/test/vm/bool.t @@ -39,12 +39,12 @@ $ NO_COLOR="1" print_instructions . Equal [CONSTANTS] - 0x00000001 (00000001) : 3 - 0x00000002 (00000002) : 5 + 0x00000000 (00000000) : 3 + 0x00000001 (00000001) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Equal 0011 I_Pop @@ -53,12 +53,12 @@ $ NO_COLOR="1" print_instructions . NotEqual [CONSTANTS] - 0x00000001 (00000001) : 3 - 0x00000002 (00000002) : 5 + 0x00000000 (00000000) : 3 + 0x00000001 (00000001) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Not_Equal 0011 I_Pop @@ -67,12 +67,12 @@ $ NO_COLOR="1" print_instructions . Greater [CONSTANTS] - 0x00000001 (00000001) : 3 - 0x00000002 (00000002) : 5 + 0x00000000 (00000000) : 3 + 0x00000001 (00000001) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Greater 0011 I_Pop @@ -81,12 +81,12 @@ $ NO_COLOR="1" print_instructions . GreaterEqual [CONSTANTS] - 0x00000001 (00000001) : 3 - 0x00000002 (00000002) : 5 + 0x00000000 (00000000) : 3 + 0x00000001 (00000001) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Greater_Equal 0011 I_Pop @@ -95,12 +95,12 @@ $ NO_COLOR="1" print_instructions . Less [CONSTANTS] - 0x00000001 (00000001) : 3 - 0x00000002 (00000002) : 5 + 0x00000000 (00000000) : 3 + 0x00000001 (00000001) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Less 0011 I_Pop @@ -109,12 +109,12 @@ $ NO_COLOR="1" print_instructions . LessEqual [CONSTANTS] - 0x00000001 (00000001) : 3 - 0x00000002 (00000002) : 5 + 0x00000000 (00000000) : 3 + 0x00000001 (00000001) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Less_Equal 0011 I_Pop diff --git a/test/vm/char.t b/test/vm/char.t index 4e80d8b..3293a8d 100644 --- a/test/vm/char.t +++ b/test/vm/char.t @@ -3,11 +3,11 @@ $ NO_COLOR="1" print_instructions . Char [CONSTANTS] - 0x00000001 (00000001) : 'c' - 0x00000002 (00000002) : 1 + 0x00000000 (00000000) : 'c' + 0x00000001 (00000001) : 1 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Add 0011 I_Pop diff --git a/test/vm/functions.pi b/test/vm/functions.pi index 240933f..8887505 100644 --- a/test/vm/functions.pi +++ b/test/vm/functions.pi @@ -22,3 +22,20 @@ component FunctionCurried { returns_returns_one()() } + +component FunctionScope { + let trap = "123"; + let globalSeed = 50; + + let minusOne = fn () -> { + let n = 1; + globalSeed - n + }; + + let minusTwo = fn () -> { + let n = 2; + globalSeed - n + }; + + minusOne() + minusTwo() +} diff --git a/test/vm/functions.t b/test/vm/functions.t index 1aa0cd3..5ce4c9b 100644 --- a/test/vm/functions.t +++ b/test/vm/functions.t @@ -3,15 +3,15 @@ $ NO_COLOR="1" print_instructions . FunctionEmpty [CONSTANTS] - 0x00000001 (00000001) : [ + 0x00000000 (00000000) : [ 0000 I_Null 0001 I_Return ] [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Set_Global 0x00000001 (00000001) - 0010 I_Get_Global 0x00000001 (00000001) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Set_Global 0x00000000 (00000000) + 0010 I_Get_Global 0x00000000 (00000000) 0015 I_Call 0016 I_Pop @@ -20,23 +20,23 @@ $ NO_COLOR="1" print_instructions . Function [CONSTANTS] - 0x00000001 (00000001) : 1 - 0x00000002 (00000002) : 2 - 0x00000003 (00000003) : [ - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Set_Global 0x00000001 (00000001) - 0010 I_Constant 0x00000002 (00000002) - 0015 I_Set_Global 0x00000002 (00000002) - 0020 I_Get_Global 0x00000001 (00000001) - 0025 I_Get_Global 0x00000002 (00000002) + 0x00000000 (00000000) : 1 + 0x00000001 (00000001) : 2 + 0x00000002 (00000002) : [ + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Set_Local 0x00000000 (00000000) + 0010 I_Constant 0x00000001 (00000001) + 0015 I_Set_Local 0x00000001 (00000001) + 0020 I_Get_Local 0x00000000 (00000000) + 0025 I_Get_Local 0x00000001 (00000001) 0030 I_Add 0031 I_Return ] [INSTRUCTIONS] - 0000 I_Constant 0x00000003 (00000003) - 0005 I_Set_Global 0x00000003 (00000003) - 0010 I_Get_Global 0x00000003 (00000003) + 0000 I_Constant 0x00000002 (00000002) + 0005 I_Set_Global 0x00000000 (00000000) + 0010 I_Get_Global 0x00000000 (00000000) 0015 I_Call 0016 I_Pop @@ -45,27 +45,69 @@ $ NO_COLOR="1" print_instructions . FunctionCurried [CONSTANTS] - 0x00000001 (00000001) : 1 - 0x00000002 (00000002) : 2 - 0x00000003 (00000003) : [ + 0x00000000 (00000000) : 1 + 0x00000001 (00000001) : 2 + 0x00000002 (00000002) : [ 0000 I_True 0001 I_Jump_If_False 0x00000010 (00000016) - 0006 I_Constant 0x00000001 (00000001) + 0006 I_Constant 0x00000000 (00000000) 0011 I_Jump 0x00000015 (00000021) - 0016 I_Constant 0x00000002 (00000002) + 0016 I_Constant 0x00000001 (00000001) 0021 I_Return ] - 0x00000004 (00000004) : [ - 0000 I_Get_Global 0x00000001 (00000001) + 0x00000003 (00000003) : [ + 0000 I_Get_Global 0x00000000 (00000000) 0005 I_Return ] [INSTRUCTIONS] - 0000 I_Constant 0x00000003 (00000003) - 0005 I_Set_Global 0x00000001 (00000001) - 0010 I_Constant 0x00000004 (00000004) - 0015 I_Set_Global 0x00000002 (00000002) - 0020 I_Get_Global 0x00000002 (00000002) + 0000 I_Constant 0x00000002 (00000002) + 0005 I_Set_Global 0x00000000 (00000000) + 0010 I_Constant 0x00000003 (00000003) + 0015 I_Set_Global 0x00000001 (00000001) + 0020 I_Get_Global 0x00000001 (00000001) 0025 I_Call 0026 I_Call 0027 I_Pop + + $ NO_COLOR="1" print_vm . FunctionScope + 97 + + $ NO_COLOR="1" print_instructions . FunctionScope + [CONSTANTS] + 0x00000000 (00000000) : "123" + 0x00000001 (00000001) : 50 + 0x00000002 (00000002) : 1 + 0x00000003 (00000003) : [ + 0000 I_Constant 0x00000002 (00000002) + 0005 I_Set_Local 0x00000000 (00000000) + 0010 I_Get_Global 0x00000001 (00000001) + 0015 I_Get_Local 0x00000000 (00000000) + 0020 I_Sub + 0021 I_Return + ] + 0x00000004 (00000004) : 2 + 0x00000005 (00000005) : [ + 0000 I_Constant 0x00000004 (00000004) + 0005 I_Set_Local 0x00000000 (00000000) + 0010 I_Get_Global 0x00000001 (00000001) + 0015 I_Get_Local 0x00000000 (00000000) + 0020 I_Sub + 0021 I_Return + ] + + [INSTRUCTIONS] + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Set_Global 0x00000000 (00000000) + 0010 I_Constant 0x00000001 (00000001) + 0015 I_Set_Global 0x00000001 (00000001) + 0020 I_Constant 0x00000003 (00000003) + 0025 I_Set_Global 0x00000002 (00000002) + 0030 I_Constant 0x00000005 (00000005) + 0035 I_Set_Global 0x00000003 (00000003) + 0040 I_Get_Global 0x00000002 (00000002) + 0045 I_Call + 0046 I_Get_Global 0x00000003 (00000003) + 0051 I_Call + 0052 I_Add + 0053 I_Pop diff --git a/test/vm/math.t b/test/vm/math.t index 65c0eba..4d505ac 100644 --- a/test/vm/math.t +++ b/test/vm/math.t @@ -3,12 +3,12 @@ $ NO_COLOR="1" print_instructions . Add [CONSTANTS] - 0x00000001 (00000001) : 7 - 0x00000002 (00000002) : 5 + 0x00000000 (00000000) : 7 + 0x00000001 (00000001) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Add 0011 I_Pop @@ -17,12 +17,12 @@ $ NO_COLOR="1" print_instructions . Sub [CONSTANTS] - 0x00000001 (00000001) : 7 - 0x00000002 (00000002) : 5 + 0x00000000 (00000000) : 7 + 0x00000001 (00000001) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Sub 0011 I_Pop @@ -31,12 +31,12 @@ $ NO_COLOR="1" print_instructions . Div [CONSTANTS] - 0x00000001 (00000001) : 7 - 0x00000002 (00000002) : 5 + 0x00000000 (00000000) : 7 + 0x00000001 (00000001) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Div 0011 I_Pop @@ -45,12 +45,12 @@ $ NO_COLOR="1" print_instructions . Mul [CONSTANTS] - 0x00000001 (00000001) : 7 - 0x00000002 (00000002) : 5 + 0x00000000 (00000000) : 7 + 0x00000001 (00000001) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Mul 0011 I_Pop @@ -59,12 +59,12 @@ $ NO_COLOR="1" print_instructions . Mod [CONSTANTS] - 0x00000001 (00000001) : 7 - 0x00000002 (00000002) : 5 + 0x00000000 (00000000) : 7 + 0x00000001 (00000001) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Mod 0011 I_Pop @@ -73,12 +73,12 @@ $ NO_COLOR="1" print_instructions . Pow [CONSTANTS] - 0x00000001 (00000001) : 7 - 0x00000002 (00000002) : 5 + 0x00000000 (00000000) : 7 + 0x00000001 (00000001) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Pow 0011 I_Pop @@ -87,10 +87,10 @@ $ NO_COLOR="1" print_instructions . MinusInt [CONSTANTS] - 0x00000001 (00000001) : 5 + 0x00000000 (00000000) : 5 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) + 0000 I_Constant 0x00000000 (00000000) 0005 I_Minus 0006 I_Pop @@ -99,10 +99,10 @@ $ NO_COLOR="1" print_instructions . MinusFloat [CONSTANTS] - 0x00000001 (00000001) : 3.140000 + 0x00000000 (00000000) : 3.140000 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) + 0000 I_Constant 0x00000000 (00000000) 0005 I_Minus 0006 I_Pop @@ -111,30 +111,30 @@ $ NO_COLOR="1" print_instructions . Math [CONSTANTS] - 0x00000001 (00000001) : 5 - 0x00000002 (00000002) : 3 - 0x00000003 (00000003) : 2 - 0x00000004 (00000004) : 6 - 0x00000005 (00000005) : 4 - 0x00000006 (00000006) : 8 + 0x00000000 (00000000) : 5 + 0x00000001 (00000001) : 3 + 0x00000002 (00000002) : 2 + 0x00000003 (00000003) : 6 + 0x00000004 (00000004) : 4 + 0x00000005 (00000005) : 8 + 0x00000006 (00000006) : 10 0x00000007 (00000007) : 10 - 0x00000008 (00000008) : 10 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Mul - 0011 I_Constant 0x00000003 (00000003) - 0016 I_Constant 0x00000004 (00000004) - 0021 I_Constant 0x00000005 (00000005) - 0026 I_Constant 0x00000006 (00000006) + 0011 I_Constant 0x00000002 (00000002) + 0016 I_Constant 0x00000003 (00000003) + 0021 I_Constant 0x00000004 (00000004) + 0026 I_Constant 0x00000005 (00000005) 0031 I_Mul 0032 I_Add - 0033 I_Constant 0x00000007 (00000007) + 0033 I_Constant 0x00000006 (00000006) 0038 I_Pow 0039 I_Div 0040 I_Add - 0041 I_Constant 0x00000008 (00000008) + 0041 I_Constant 0x00000007 (00000007) 0046 I_Minus 0047 I_Add 0048 I_Pop diff --git a/test/vm/record.t b/test/vm/record.t index 040ace8..ae8ef00 100644 --- a/test/vm/record.t +++ b/test/vm/record.t @@ -6,23 +6,23 @@ $ NO_COLOR="1" print_instructions . Record [CONSTANTS] - 0x00000001 (00000001) : "a" - 0x00000002 (00000002) : "b" - 0x00000003 (00000003) : "c" - 0x00000004 (00000004) : "d" - 0x00000005 (00000005) : 1 - 0x00000006 (00000006) : "foo" - 0x00000007 (00000007) : 3.141500 + 0x00000000 (00000000) : "a" + 0x00000001 (00000001) : "b" + 0x00000002 (00000002) : "c" + 0x00000003 (00000003) : "d" + 0x00000004 (00000004) : 1 + 0x00000005 (00000005) : "foo" + 0x00000006 (00000006) : 3.141500 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) - 0010 I_Constant 0x00000003 (00000003) - 0015 I_Constant 0x00000004 (00000004) - 0020 I_Constant 0x00000005 (00000005) - 0025 I_Constant 0x00000006 (00000006) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) + 0010 I_Constant 0x00000002 (00000002) + 0015 I_Constant 0x00000003 (00000003) + 0020 I_Constant 0x00000004 (00000004) + 0025 I_Constant 0x00000005 (00000005) 0030 I_True - 0031 I_Constant 0x00000007 (00000007) + 0031 I_Constant 0x00000006 (00000006) 0036 I_Record 4 0041 I_Pop @@ -44,46 +44,46 @@ $ NO_COLOR="1" print_instructions . RecordNested [CONSTANTS] - 0x00000001 (00000001) : "a" - 0x00000002 (00000002) : "b" - 0x00000003 (00000003) : "c" - 0x00000004 (00000004) : "d" - 0x00000005 (00000005) : 1 - 0x00000006 (00000006) : "foo" - 0x00000007 (00000007) : "e" - 0x00000008 (00000008) : "f" - 0x00000009 (00000009) : "g" - 0x0000000A (00000010) : "123" - 0x0000000B (00000011) : 321 - 0x0000000C (00000012) : 1 - 0x0000000D (00000013) : 2 - 0x0000000E (00000014) : 3 - 0x0000000F (00000015) : 4 - 0x00000010 (00000016) : 5 - 0x00000011 (00000017) : 6 - 0x00000012 (00000018) : 7 - 0x00000013 (00000019) : 8 + 0x00000000 (00000000) : "a" + 0x00000001 (00000001) : "b" + 0x00000002 (00000002) : "c" + 0x00000003 (00000003) : "d" + 0x00000004 (00000004) : 1 + 0x00000005 (00000005) : "foo" + 0x00000006 (00000006) : "e" + 0x00000007 (00000007) : "f" + 0x00000008 (00000008) : "g" + 0x00000009 (00000009) : "123" + 0x0000000A (00000010) : 321 + 0x0000000B (00000011) : 1 + 0x0000000C (00000012) : 2 + 0x0000000D (00000013) : 3 + 0x0000000E (00000014) : 4 + 0x0000000F (00000015) : 5 + 0x00000010 (00000016) : 6 + 0x00000011 (00000017) : 7 + 0x00000012 (00000018) : 8 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) - 0010 I_Constant 0x00000003 (00000003) - 0015 I_Constant 0x00000004 (00000004) - 0020 I_Constant 0x00000005 (00000005) - 0025 I_Constant 0x00000006 (00000006) - 0030 I_Constant 0x00000007 (00000007) - 0035 I_Constant 0x00000008 (00000008) - 0040 I_Constant 0x00000009 (00000009) - 0045 I_Constant 0x0000000A (00000010) - 0050 I_Constant 0x0000000B (00000011) - 0055 I_Constant 0x0000000C (00000012) - 0060 I_Constant 0x0000000D (00000013) - 0065 I_Constant 0x0000000E (00000014) - 0070 I_Constant 0x0000000F (00000015) - 0075 I_Constant 0x00000010 (00000016) - 0080 I_Constant 0x00000011 (00000017) - 0085 I_Constant 0x00000012 (00000018) - 0090 I_Constant 0x00000013 (00000019) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) + 0010 I_Constant 0x00000002 (00000002) + 0015 I_Constant 0x00000003 (00000003) + 0020 I_Constant 0x00000004 (00000004) + 0025 I_Constant 0x00000005 (00000005) + 0030 I_Constant 0x00000006 (00000006) + 0035 I_Constant 0x00000007 (00000007) + 0040 I_Constant 0x00000008 (00000008) + 0045 I_Constant 0x00000009 (00000009) + 0050 I_Constant 0x0000000A (00000010) + 0055 I_Constant 0x0000000B (00000011) + 0060 I_Constant 0x0000000C (00000012) + 0065 I_Constant 0x0000000D (00000013) + 0070 I_Constant 0x0000000E (00000014) + 0075 I_Constant 0x0000000F (00000015) + 0080 I_Constant 0x00000010 (00000016) + 0085 I_Constant 0x00000011 (00000017) + 0090 I_Constant 0x00000012 (00000018) 0095 I_Array 8 0100 I_Record 3 0105 I_True @@ -100,46 +100,46 @@ $ NO_COLOR="1" print_instructions . RecordNested [CONSTANTS] - 0x00000001 (00000001) : "a" - 0x00000002 (00000002) : "b" - 0x00000003 (00000003) : "c" - 0x00000004 (00000004) : "d" - 0x00000005 (00000005) : 1 - 0x00000006 (00000006) : "foo" - 0x00000007 (00000007) : "e" - 0x00000008 (00000008) : "f" - 0x00000009 (00000009) : "g" - 0x0000000A (00000010) : "123" - 0x0000000B (00000011) : 321 - 0x0000000C (00000012) : 1 - 0x0000000D (00000013) : 2 - 0x0000000E (00000014) : 3 - 0x0000000F (00000015) : 4 - 0x00000010 (00000016) : 5 - 0x00000011 (00000017) : 6 - 0x00000012 (00000018) : 7 - 0x00000013 (00000019) : 8 + 0x00000000 (00000000) : "a" + 0x00000001 (00000001) : "b" + 0x00000002 (00000002) : "c" + 0x00000003 (00000003) : "d" + 0x00000004 (00000004) : 1 + 0x00000005 (00000005) : "foo" + 0x00000006 (00000006) : "e" + 0x00000007 (00000007) : "f" + 0x00000008 (00000008) : "g" + 0x00000009 (00000009) : "123" + 0x0000000A (00000010) : 321 + 0x0000000B (00000011) : 1 + 0x0000000C (00000012) : 2 + 0x0000000D (00000013) : 3 + 0x0000000E (00000014) : 4 + 0x0000000F (00000015) : 5 + 0x00000010 (00000016) : 6 + 0x00000011 (00000017) : 7 + 0x00000012 (00000018) : 8 [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) - 0010 I_Constant 0x00000003 (00000003) - 0015 I_Constant 0x00000004 (00000004) - 0020 I_Constant 0x00000005 (00000005) - 0025 I_Constant 0x00000006 (00000006) - 0030 I_Constant 0x00000007 (00000007) - 0035 I_Constant 0x00000008 (00000008) - 0040 I_Constant 0x00000009 (00000009) - 0045 I_Constant 0x0000000A (00000010) - 0050 I_Constant 0x0000000B (00000011) - 0055 I_Constant 0x0000000C (00000012) - 0060 I_Constant 0x0000000D (00000013) - 0065 I_Constant 0x0000000E (00000014) - 0070 I_Constant 0x0000000F (00000015) - 0075 I_Constant 0x00000010 (00000016) - 0080 I_Constant 0x00000011 (00000017) - 0085 I_Constant 0x00000012 (00000018) - 0090 I_Constant 0x00000013 (00000019) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) + 0010 I_Constant 0x00000002 (00000002) + 0015 I_Constant 0x00000003 (00000003) + 0020 I_Constant 0x00000004 (00000004) + 0025 I_Constant 0x00000005 (00000005) + 0030 I_Constant 0x00000006 (00000006) + 0035 I_Constant 0x00000007 (00000007) + 0040 I_Constant 0x00000008 (00000008) + 0045 I_Constant 0x00000009 (00000009) + 0050 I_Constant 0x0000000A (00000010) + 0055 I_Constant 0x0000000B (00000011) + 0060 I_Constant 0x0000000C (00000012) + 0065 I_Constant 0x0000000D (00000013) + 0070 I_Constant 0x0000000E (00000014) + 0075 I_Constant 0x0000000F (00000015) + 0080 I_Constant 0x00000010 (00000016) + 0085 I_Constant 0x00000011 (00000017) + 0090 I_Constant 0x00000012 (00000018) 0095 I_Array 8 0100 I_Record 3 0105 I_True @@ -151,27 +151,27 @@ $ NO_COLOR="1" print_instructions . RecordAccessDot [CONSTANTS] - 0x00000001 (00000001) : "a" - 0x00000002 (00000002) : "c" - 0x00000003 (00000003) : 1 - 0x00000004 (00000004) : "e" - 0x00000005 (00000005) : "123" - 0x00000006 (00000006) : "c" - 0x00000007 (00000007) : "e" + 0x00000000 (00000000) : "a" + 0x00000001 (00000001) : "c" + 0x00000002 (00000002) : 1 + 0x00000003 (00000003) : "e" + 0x00000004 (00000004) : "123" + 0x00000005 (00000005) : "c" + 0x00000006 (00000006) : "e" [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) - 0010 I_Constant 0x00000003 (00000003) - 0015 I_Constant 0x00000004 (00000004) - 0020 I_Constant 0x00000005 (00000005) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) + 0010 I_Constant 0x00000002 (00000002) + 0015 I_Constant 0x00000003 (00000003) + 0020 I_Constant 0x00000004 (00000004) 0025 I_Record 1 0030 I_Record 2 - 0035 I_Set_Global 0x00000001 (00000001) - 0040 I_Get_Global 0x00000001 (00000001) - 0045 I_Constant 0x00000006 (00000006) + 0035 I_Set_Global 0x00000000 (00000000) + 0040 I_Get_Global 0x00000000 (00000000) + 0045 I_Constant 0x00000005 (00000005) 0050 I_Dot_Index - 0051 I_Constant 0x00000007 (00000007) + 0051 I_Constant 0x00000006 (00000006) 0056 I_Dot_Index 0057 I_Pop @@ -180,26 +180,26 @@ $ NO_COLOR="1" print_instructions . RecordAccessBracket [CONSTANTS] - 0x00000001 (00000001) : "a" - 0x00000002 (00000002) : "c" - 0x00000003 (00000003) : 1 - 0x00000004 (00000004) : "e" - 0x00000005 (00000005) : "123" - 0x00000006 (00000006) : "c" - 0x00000007 (00000007) : "e" + 0x00000000 (00000000) : "a" + 0x00000001 (00000001) : "c" + 0x00000002 (00000002) : 1 + 0x00000003 (00000003) : "e" + 0x00000004 (00000004) : "123" + 0x00000005 (00000005) : "c" + 0x00000006 (00000006) : "e" [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) - 0010 I_Constant 0x00000003 (00000003) - 0015 I_Constant 0x00000004 (00000004) - 0020 I_Constant 0x00000005 (00000005) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) + 0010 I_Constant 0x00000002 (00000002) + 0015 I_Constant 0x00000003 (00000003) + 0020 I_Constant 0x00000004 (00000004) 0025 I_Record 1 0030 I_Record 2 - 0035 I_Set_Global 0x00000001 (00000001) - 0040 I_Get_Global 0x00000001 (00000001) - 0045 I_Constant 0x00000006 (00000006) + 0035 I_Set_Global 0x00000000 (00000000) + 0040 I_Get_Global 0x00000000 (00000000) + 0045 I_Constant 0x00000005 (00000005) 0050 I_Index - 0051 I_Constant 0x00000007 (00000007) + 0051 I_Constant 0x00000006 (00000006) 0056 I_Index 0057 I_Pop diff --git a/test/vm/strings.t b/test/vm/strings.t index adfe2d2..fab4fcd 100644 --- a/test/vm/strings.t +++ b/test/vm/strings.t @@ -3,10 +3,10 @@ $ NO_COLOR="1" print_instructions . String [CONSTANTS] - 0x00000001 (00000001) : "Hello!" + 0x00000000 (00000000) : "Hello!" [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) + 0000 I_Constant 0x00000000 (00000000) 0005 I_Pop $ NO_COLOR="1" print_vm . StringConcat @@ -14,15 +14,15 @@ $ NO_COLOR="1" print_instructions . StringConcat [CONSTANTS] - 0x00000001 (00000001) : "Hello " - 0x00000002 (00000002) : "World" - 0x00000003 (00000003) : "!" + 0x00000000 (00000000) : "Hello " + 0x00000001 (00000001) : "World" + 0x00000002 (00000002) : "!" [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Constant 0x00000002 (00000002) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Constant 0x00000001 (00000001) 0010 I_Concat - 0011 I_Constant 0x00000003 (00000003) + 0011 I_Constant 0x00000002 (00000002) 0016 I_Concat 0017 I_Pop @@ -31,16 +31,16 @@ $ NO_COLOR="1" print_instructions . StringInterpolation [CONSTANTS] - 0x00000001 (00000001) : "World" - 0x00000002 (00000002) : "Hello " - 0x00000003 (00000003) : "!" + 0x00000000 (00000000) : "World" + 0x00000001 (00000001) : "Hello " + 0x00000002 (00000002) : "!" [INSTRUCTIONS] - 0000 I_Constant 0x00000001 (00000001) - 0005 I_Set_Global 0x00000001 (00000001) - 0010 I_Constant 0x00000002 (00000002) - 0015 I_Get_Global 0x00000001 (00000001) + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Set_Global 0x00000000 (00000000) + 0010 I_Constant 0x00000001 (00000001) + 0015 I_Get_Global 0x00000000 (00000000) 0020 I_Concat - 0021 I_Constant 0x00000003 (00000003) + 0021 I_Constant 0x00000002 (00000002) 0026 I_Concat 0027 I_Pop