Phasor 2.2.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
Phasor Namespace Reference

The Phasor Programming Language and Runtime. More...

Namespaces

namespace  AST
 Abstract Syntax Tree (AST) namespace.
namespace  Frontend
 Frontend namespace.

Classes

struct  Token
class  Lexer
 Lexer. More...
class  Parser
 Parser. More...
class  Compiler
 CLI wrapper for bytecode generation from Phasor source. More...
class  CppCompiler
 CLI wrapper for C++ code generation from Phasor source. More...
struct  AppArgs
class  Repl
 Read-Eval-Print Loop for Phasor Programming Language. More...
class  BinaryRuntime
 CLI wrapper for running Phasor bytecode binaries. More...
class  NativeRuntime
 CLI wrapper for running Phasor scripts and bytecode in-process. More...
class  ScriptingRuntime
 CLI wrapper for running Phasor scripts. More...
class  BytecodeDeserializer
 Bytecode binary format deserializer. More...
class  BytecodeSerializer
 Bytecode binary format serializer. More...
struct  Instruction
 Instruction with up to 5 operands Format: instruction operand1, operand2, operand3, operand4, operand5 Each instruction uses only the operands it needs. More...
struct  StructInfo
 Struct metadata stored alongside bytecode (struct section). More...
struct  Bytecode
 Complete bytecode structure. More...
class  CodeGenerator
 Code generator for Phasor VM. More...
class  CppCodeGenerator
 Generates C++ header files with embedded Phasor bytecode. More...
class  PhasorIR
 Phasor IR Serializer/Deserializer. More...
struct  Plugin
 Represents a loaded plugin. More...
class  FFI
 Manages loading, registering, and unloading native FFI plugins. More...
class  StdLib
 Standard library Contains all the standard library functions. More...
class  Value
 A value in the Phasor VM. More...
class  VM
 Virtual Machine. More...

Typedefs

using NativeFunction = std::function<Value(const std::vector<Value> &args, VM *vm)>
 Native function signature.

Enumerations

enum class  TokenType {
  Identifier , Number , String , Keyword ,
  Symbol , EndOfFile , Unknown
}
enum class  OpCode : uint8_t {
  PUSH_CONST , POP , IADD , ISUBTRACT ,
  IMULTIPLY , IDIVIDE , IMODULO , FLADD ,
  FLSUBTRACT , FLMULTIPLY , FLDIVIDE , FLMODULO ,
  SQRT , POW , LOG , EXP ,
  SIN , COS , TAN , NEGATE ,
  NOT , IAND , IOR , FLAND ,
  FLOR , IEQUAL , INOT_EQUAL , ILESS_THAN ,
  IGREATER_THAN , ILESS_EQUAL , IGREATER_EQUAL , FLEQUAL ,
  FLNOT_EQUAL , FLLESS_THAN , FLGREATER_THAN , FLLESS_EQUAL ,
  FLGREATER_EQUAL , JUMP , JUMP_IF_FALSE , JUMP_IF_TRUE ,
  JUMP_BACK , STORE_VAR , LOAD_VAR , PRINT ,
  PRINTERROR , READLINE , IMPORT , HALT ,
  CALL_NATIVE , CALL , SYSTEM , SYSTEM_OUT ,
  SYSTEM_ERR , RETURN , TRUE_P , FALSE_P ,
  NULL_VAL , LEN , CHAR_AT , SUBSTR ,
  NEW_STRUCT , GET_FIELD , SET_FIELD , NEW_STRUCT_INSTANCE_STATIC ,
  GET_FIELD_STATIC , SET_FIELD_STATIC , MOV , LOAD_CONST_R ,
  LOAD_VAR_R , STORE_VAR_R , PUSH_R , PUSH2_R ,
  POP_R , POP2_R , IADD_R , ISUB_R ,
  IMUL_R , IDIV_R , IMOD_R , FLADD_R ,
  FLSUB_R , FLMUL_R , FLDIV_R , FLMOD_R ,
  SQRT_R , POW_R , LOG_R , EXP_R ,
  SIN_R , COS_R , TAN_R , IAND_R ,
  IOR_R , IEQ_R , INE_R , ILT_R ,
  IGT_R , ILE_R , IGE_R , FLAND_R ,
  FLOR_R , FLEQ_R , FLNE_R , FLLT_R ,
  FLGT_R , FLLE_R , FLGE_R , NEG_R ,
  NOT_R , PRINT_R , PRINTERROR_R , READLINE_R ,
  SYSTEM_R , SYSTEM_OUT_R , SYSTEM_ERR_R
}
enum class  ValueType {
  Null , Bool , Int , Float ,
  String , Struct , Array
}
 Runtime value types for the VM. More...

Functions

static int hexValue (char c)
static Phasor::Value from_c_value (const PhasorValue &c_value)
 Converts a C-style FFI value to a C++ VM value.
static PhasorValue to_c_value (const Phasor::Value &cpp_value, std::vector< std::unique_ptr< char[]> > &string_arena, std::vector< std::unique_ptr< PhasorValue[]> > &array_arena)
 Converts a C++ VM value to a C-style FFI value.
static Phasor::Value c_native_func_wrapper (PhasorNativeFunction c_func, Phasor::VM *vm, const std::vector< Phasor::Value > &args)
 The "trampoline" that wraps a C function from a plugin.
static void register_native_c_func (PhasorVM *vm, const char *name, PhasorNativeFunction func)
 The concrete implementation of the PhasorRegisterFunction API call.

Variables

static std::vector< std::string > sbPool
static std::vector< size_t > sbFreeIndices

Detailed Description

The Phasor Programming Language and Runtime.

Typedef Documentation

◆ NativeFunction

using Phasor::NativeFunction = std::function<Value(const std::vector<Value> &args, VM *vm)>

Native function signature.

Definition at line 19 of file StdLib.hpp.

Enumeration Type Documentation

◆ OpCode

enum class Phasor::OpCode : uint8_t
strong
Enumerator
PUSH_CONST 

Push constant from constant pool.

POP 

Pop top of stack.

IADD 

Pop b, pop a, push a + b.

ISUBTRACT 

Pop b, pop a, push a - b.

IMULTIPLY 

Pop b, pop a, push a * b.

IDIVIDE 

Pop b, pop a, push a / b.

IMODULO 

Pop b, pop a, push a % b.

FLADD 

Pop b, pop a, push a + b.

FLSUBTRACT 

Pop b, pop a, push a - b.

FLMULTIPLY 

Pop b, pop a, push a * b.

FLDIVIDE 

Pop b, pop a, push a / b.

FLMODULO 

Pop b, pop a, push a % b.

SQRT 

sqrt()

POW 

pow()

LOG 

log()

EXP 

exp()

SIN 

sin()

COS 

cos()

TAN 

tan()

NEGATE 

Pop a, push -a.

NOT 

Pop a, push !a.

IAND 

Pop b, pop a, push a && b.

IOR 

Pop b, pop a, push a || b.

FLAND 

Pop b, pop a, push a && b.

FLOR 

Pop b, pop a, push a || b.

IEQUAL 

Pop b, pop a, push a == b.

INOT_EQUAL 

Pop b, pop a, push a != b.

ILESS_THAN 

Pop b, pop a, push a < b.

IGREATER_THAN 

Pop b, pop a, push a > b.

ILESS_EQUAL 

Pop b, pop a, push a <= b.

IGREATER_EQUAL 

Pop b, pop a, push a >= b.

FLEQUAL 

Pop b, pop a, push a == b.

FLNOT_EQUAL 

Pop b, pop a, push a != b.

FLLESS_THAN 

Pop b, pop a, push a < b.

FLGREATER_THAN 

Pop b, pop a, push a > b.

FLLESS_EQUAL 

Pop b, pop a, push a <= b.

FLGREATER_EQUAL 

Pop b, pop a, push a >= b.

JUMP 

Unconditional jump to offset.

JUMP_IF_FALSE 

Jump if top of stack is false (pops value).

JUMP_IF_TRUE 

Jump if top of stack is true (pops value).

JUMP_BACK 

Jump backwards (for loops).

STORE_VAR 

Pop top of stack, store in variable slot.

LOAD_VAR 

Push variable value onto stack.

PRINT 

Pop top of stack and print.

PRINTERROR 

Pop top of stack and print to stderr.

READLINE 

Read line from input and push onto stack.

IMPORT 

Import a module: operand is index of module path in constants.

HALT 

Stop execution.

CALL_NATIVE 

Call a native function: operand is index of function name in constants.

CALL 

Call a user function: operand is index of function name in constants.

SYSTEM 

Call a system function: operand is index of function name in constants.

SYSTEM_OUT 

Call system function and push stdout.

SYSTEM_ERR 

Call system function and push stderr.

RETURN 

Return from function.

TRUE_P 

Push true.

FALSE_P 

Push false.

NULL_VAL 

Push null.

LEN 

Pop s, push len(s).

CHAR_AT 

Pop index, pop s, push s[index].

SUBSTR 

Pop len, pop start, pop s, push s.substr(start, len).

NEW_STRUCT 

Create new struct: operand is index of struct name in constants.

GET_FIELD 

Pop struct, pop field name, push field value.

SET_FIELD 

Pop struct, pop field name, pop value, set field value.

NEW_STRUCT_INSTANCE_STATIC 

Create new struct instance using struct section metadata (structIndex).

GET_FIELD_STATIC 

Pop struct instance, push field by static offset (structIndex, fieldOffset).

SET_FIELD_STATIC 

Pop value and struct instance, set field by static offset.

MOV 

Copy register to register: R[rA] = R[rB].

LOAD_CONST_R 

Load constant to register: R[rA] = constants[immediate].

LOAD_VAR_R 

Load variable to register: R[rA] = variables[immediate].

STORE_VAR_R 

Store register to variable: variables[immediate] = R[rA].

PUSH_R 

Push register to stack: push(R[rA]).

PUSH2_R 

Push 2 registers to stack: push2(R[rA], R[rB]).

POP_R 

Pop stack to register: R[rA] = pop().

POP2_R 

Pop 2 values from stack to registers: pop2(R[rA], R[rB]).

IADD_R 

R[rA] = R[rB] + R[rC].

ISUB_R 

R[rA] = R[rB] - R[rC].

IMUL_R 

R[rA] = R[rB] * R[rC].

IDIV_R 

R[rA] = R[rB] / R[rC].

IMOD_R 

R[rA] = R[rB] % R[rC].

FLADD_R 

R[rA] = R[rB] + R[rC].

FLSUB_R 

R[rA] = R[rB] - R[rC].

FLMUL_R 

R[rA] = R[rB] * R[rC].

FLDIV_R 

R[rA] = R[rB] / R[rC].

FLMOD_R 

R[rA] = R[rB] % R[rC].

SQRT_R 

R[rA] = sqrt(R[rB]).

POW_R 

R[rA] = pow(R[rB], R[rC]).

LOG_R 

R[rA] = log(R[rB]).

EXP_R 

R[rA] = exp(R[rB]).

SIN_R 

R[rA] = sin(R[rB]).

COS_R 

R[rA] = cos(R[rB]).

TAN_R 

R[rA] = tan(R[rB]).

IAND_R 

R[rA] = R[rB] && R[rC].

IOR_R 

R[rA] = R[rB] || R[rC].

IEQ_R 

R[rA] = R[rB] == R[rC].

INE_R 

R[rA] = R[rB] != R[rC].

ILT_R 

R[rA] = R[rB] < R[rC].

IGT_R 

R[rA] = R[rB] > R[rC].

ILE_R 

R[rA] = R[rB] <= R[rC].

IGE_R 

R[rA] = R[rB] >= R[rC].

FLAND_R 

R[rA] = R[rB] && R[rC].

FLOR_R 

R[rA] = R[rB] || R[rC].

FLEQ_R 

R[rA] = R[rB] == R[rC].

FLNE_R 

R[rA] = R[rB] != R[rC].

FLLT_R 

R[rA] = R[rB] < R[rC].

FLGT_R 

R[rA] = R[rB] > R[rC].

FLLE_R 

R[rA] = R[rB] <= R[rC].

FLGE_R 

R[rA] = R[rB] >= R[rC].

NEG_R 

R[rA] = -R[rB].

NOT_R 

R[rA] = !R[rB].

PRINT_R 

Print register: print(R[rA]).

PRINTERROR_R 

Print register to stderr: printerror(R[rA]).

READLINE_R 

Read line into register: readline(R[rA]).

SYSTEM_R 

Run an operating system shell command: system(R[rA]).

SYSTEM_OUT_R 
SYSTEM_ERR_R 

Run shell command and get output: system_out(R[rA], R[rB]).

Run shell command and get error output: system_err(R[rA], R[rB])

Definition at line 15 of file CodeGen.hpp.

◆ TokenType

enum class Phasor::TokenType
strong
Enumerator
Identifier 
Number 
String 
Keyword 
Symbol 
EndOfFile 
Unknown 

Definition at line 9 of file Lexer.hpp.

◆ ValueType

enum class Phasor::ValueType
strong

Runtime value types for the VM.

Enumerator
Null 
Bool 
Int 
Float 
String 
Struct 
Array 

Definition at line 16 of file Value.hpp.

Function Documentation

◆ c_native_func_wrapper()

Phasor::Value Phasor::c_native_func_wrapper ( PhasorNativeFunction c_func,
Phasor::VM * vm,
const std::vector< Phasor::Value > & args )
static

The "trampoline" that wraps a C function from a plugin.

This function is the bridge between the C++ VM and the C plugin. It is called by the VM and is responsible for:

  1. Converting C++ arguments to C arguments, with production-ready memory management for strings.
  2. Calling the plugin's C function.
  3. Converting the C return value back to a C++ value.

Definition at line 128 of file ffi.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ from_c_value()

Phasor::Value Phasor::from_c_value ( const PhasorValue & c_value)
static

Converts a C-style FFI value to a C++ VM value.

Parameters
c_valueThe C-style value from the plugin.
Returns
The equivalent C++ value for the VM.
Note
This function is safe for strings, as it copies the C string into a new C++ std::string, taking ownership of the memory.

Definition at line 28 of file ffi.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hexValue()

int Phasor::hexValue ( char c)
static

Definition at line 207 of file Lexer.cpp.

Here is the caller graph for this function:

◆ register_native_c_func()

void Phasor::register_native_c_func ( PhasorVM * vm,
const char * name,
PhasorNativeFunction func )
static

The concrete implementation of the PhasorRegisterFunction API call.

This function is passed to the plugin. When the plugin calls it, this function creates a C++ lambda that wraps the plugin's C function pointer and registers that lambda with the Phasor VM.

Definition at line 152 of file ffi.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_c_value()

PhasorValue Phasor::to_c_value ( const Phasor::Value & cpp_value,
std::vector< std::unique_ptr< char[]> > & string_arena,
std::vector< std::unique_ptr< PhasorValue[]> > & array_arena )
static

Converts a C++ VM value to a C-style FFI value.

Parameters
cpp_valueThe C++ value from the VM.
string_arenaA vector of unique_ptrs to manage the lifetime of C strings. Any strings converted will be allocated and their memory will be managed by this arena.
Returns
The equivalent C-style value for the plugin.

Definition at line 69 of file ffi.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ sbFreeIndices

std::vector<size_t> Phasor::sbFreeIndices
static

Definition at line 30 of file string.cpp.

◆ sbPool

std::vector<std::string> Phasor::sbPool
static

Definition at line 29 of file string.cpp.