Skip to content

Add builtin function call convention to bytecode VM #51

@synapticvoid

Description

@synapticvoid

Summary

Bridge existing builtin functions (print, toString, etc.) into the VM call convention so they can be invoked like user functions.

Checklist

Value representation

  • Add BuiltinFn (or Function with builtin/user variants) to the VM Value type
  • Register builtins into the VM globals at startup (mirror builtins.registerAll)

Compiler

  • Resolve builtin names as globals (not locals) during compilation
  • Compile builtin calls through the same fn_call path — emit args + op_call

VM

  • In the op_call handler, branch on callable type: user fn → push call frame, builtin fn → invoke directly with evaluated args
  • Pass RuntimeContext to builtins (needed for stdout/stderr access)
  • Handle return value from builtins (push onto stack)

toString coercion

  • Before passing struct instances to builtins, check for a qualified TypeName.toString method and call it (mirrors interpreter behavior in evaluateFnCall)

Tests

  • print(42) produces correct output via VM
  • print("hello") works
  • Builtin with return value (e.g. toString) pushes result onto stack
  • Struct instance passed to print triggers toString coercion

Depends on #43 (call frames) being merged first.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions