ucg/src/build/opcode/mod.rs

142 lines
2.8 KiB
Rust
Raw Normal View History

// Copyright 2019 Jeremy Wall
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::rc::Rc;
mod cache;
pub mod pointer;
mod runtime;
2019-07-01 15:27:09 -05:00
pub mod scope;
2019-07-13 10:59:10 -05:00
mod vm;
2019-07-01 15:27:09 -05:00
2019-07-13 10:59:10 -05:00
pub use vm::VM;
use pointer::OpPointer;
2019-07-01 15:27:09 -05:00
use scope::Stack;
#[derive(Debug, PartialEq, Clone)]
pub enum Primitive {
// Primitive Types
Int(i64),
Float(f64),
Str(String),
Bool(bool),
Empty,
}
#[derive(Debug, PartialEq, Clone)]
pub enum Composite {
List(Vec<Rc<Value>>),
Tuple(Vec<(String, Rc<Value>)>),
}
2019-07-01 15:27:09 -05:00
#[derive(Debug, PartialEq, Clone)]
pub struct Func {
ptr: OpPointer,
2019-07-01 15:27:09 -05:00
bindings: Vec<String>,
snapshot: Stack,
}
#[derive(Debug, PartialEq, Clone)]
pub struct Module {
ptr: OpPointer,
2019-07-01 15:27:09 -05:00
result_ptr: Option<usize>,
flds: Vec<(String, Rc<Value>)>,
2019-07-01 15:27:09 -05:00
}
#[derive(Debug, PartialEq, Clone)]
pub enum Value {
// Binding names.
S(String),
// Primitive Types
P(Primitive),
// Composite Types.
C(Composite),
// Program Pointer
T(usize),
2019-07-01 15:27:09 -05:00
// Function
F(Func),
2019-07-01 15:27:09 -05:00
// Module
M(Module),
}
#[derive(Debug, PartialEq, Clone)]
pub enum Hook {
Map,
Include,
Filter,
Reduce,
Import,
Out,
Assert,
Convert,
}
#[derive(Debug, PartialEq, Clone)]
pub enum Op {
// Stack and Name manipulation.
Bind, // Bind a Val to a name in the heap
Pop, // Pop a Value off the value stack and discard it.
// Math ops
Add,
Sub,
Div,
Mul,
2019-06-30 22:51:47 -05:00
// Comparison Ops
Equal,
Gt,
Lt,
GtEq,
LtEq,
// Primitive Types ops
Val(Primitive),
// A bareword for use in bindings or lookups
Sym(String),
2019-07-01 15:27:09 -05:00
// Reference a binding on the heap
DeRef(String),
// Complex Type ops
InitTuple,
2019-06-30 22:21:28 -05:00
Field,
InitList,
Element,
2019-07-01 15:27:09 -05:00
// Copy Operation
Cp,
2019-06-30 22:21:28 -05:00
// Control Flow
Bang,
Jump(i32),
JumpIfTrue(i32),
JumpIfFalse(i32),
2019-07-10 18:20:01 -05:00
SelectJump(i32),
// FIXME(jwall): Short circuiting operations
2019-07-01 15:27:09 -05:00
// - And(usize)
// - Or(usize)
// Spacer operation, Does nothing.
2019-07-11 19:28:55 -05:00
Index, // indexing operation
2019-06-30 22:21:28 -05:00
Noop,
// Pending Computation
InitThunk(i32), // Basically just used for module return expressions
2019-07-01 15:27:09 -05:00
Module(usize),
Func(usize),
Return,
// Calls
2019-07-01 15:27:09 -05:00
FCall,
// Runtime hooks
Runtime(Hook),
}
#[derive(Debug)]
pub struct Error {}
#[cfg(test)]
mod test;