Skip to content

Commit

Permalink
working value
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenthz committed Dec 18, 2023
1 parent f820d63 commit 7cc2b13
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 163 deletions.
35 changes: 26 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod value;

use hashbrown::HashMap;
use value::Value;
use werbolg_compile::{code_dump, compile, symbols::IdVec, Environment};
use werbolg_compile::{code_dump, compile, symbols::IdVec, CompilationError, Environment};
use werbolg_core::{ConstrId, Ident, Literal, NifId, ValueFun};

Check warning on line 7 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest)

unused imports: `ConstrId`, `ValueFun`

Check warning on line 7 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused imports: `ConstrId`, `ValueFun`

Check warning on line 7 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest)

unused imports: `ConstrId`, `ValueFun`

Check warning on line 7 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest)

unused imports: `ConstrId`, `ValueFun`

Check warning on line 7 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused imports: `ConstrId`, `ValueFun`

Check warning on line 7 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused imports: `ConstrId`, `ValueFun`
use werbolg_exec::{
ExecutionEnviron, ExecutionError, ExecutionMachine, ExecutionParams, NIFCall, Valuable, NIF,
Expand Down Expand Up @@ -73,18 +73,35 @@ fn nif_hashtable_get(args: &[Value]) -> Result<Value, ExecutionError> {
}
}

fn literal_to_value(lit: &Literal) -> Value {
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum MyLiteral {
Bool(bool),
Int(u64),
}

fn literal_to_value(lit: &MyLiteral) -> Value {
match lit {
Literal::Bool(b) => Value::Bool(true),
Literal::String(_) => Value::Unit,
Literal::Number(_) => Value::Integral(0),
Literal::Decimal(_) => Value::Unit,
Literal::Bytes(_) => Value::Unit,
MyLiteral::Bool(b) => Value::Bool(*b),
MyLiteral::Int(n) => Value::Integral(*n),
}
}

fn literal_mapper(lit: Literal) -> Literal {
lit
fn literal_mapper(lit: Literal) -> Result<MyLiteral, CompilationError> {
match lit {
Literal::Bool(b) => {
let b = b.as_ref() == "true";
Ok(MyLiteral::Bool(b))
}
Literal::String(_) => Err(CompilationError::LiteralNotSupported(lit)),
Literal::Number(s) => {
let Ok(v) = u64::from_str_radix(s.as_ref(), 10) else {
todo!()
};
Ok(MyLiteral::Int(v))
}
Literal::Decimal(_) => Err(CompilationError::LiteralNotSupported(lit)),
Literal::Bytes(_) => Err(CompilationError::LiteralNotSupported(lit)),
}
}

fn get_content(args: &[String]) -> Result<(FileUnit, lang::Lang), ()> {
Expand Down
16 changes: 8 additions & 8 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ pub enum Value {
// Simple values
Bool(bool),
Integral(u64),
Binary(Box<[u8]>),
//Binary(Box<[u8]>),
HashMap(HashMap<u32, u64>),
// Composite
List(Box<[Value]>),
Struct(ConstrId, Box<[Value]>),
Enum(u32, Box<[Value]>),
//List(Box<[Value]>),
//Struct(ConstrId, Box<[Value]>),
//Enum(u32, Box<[Value]>),
// Functions
Fun(ValueFun),
}
Expand All @@ -25,10 +25,10 @@ impl Value {
Value::Bool(_) => b" bool",
Value::HashMap(_) => b" hashmap",
Value::Integral(_) => b" int",
Value::Binary(_) => b" binary",
Value::List(_) => b" list",
Value::Struct(_, _) => b" struct",
Value::Enum(_, _) => b" enum",
//Value::Binary(_) => b" binary",
//Value::List(_) => b" list",
//Value::Struct(_, _) => b" struct",
//Value::Enum(_, _) => b" enum",
Value::Fun(_) => b" fun",
}
}
Expand Down
2 changes: 1 addition & 1 deletion werbolg-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fn rewrite_expr2<'a, L: Clone + Eq + core::hash::Hash>(
) -> Result<(), CompilationError> {
match expr {
ir::Expr::Literal(_span, lit) => {
let lit_id = state.lits.add((state.params.literal_mapper)(lit));
let lit_id = state.lits.add((state.params.literal_mapper)(lit)?);
state.write_code().push(Instruction::PushLiteral(lit_id));
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion werbolg-compile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use params::CompilationParams;
use compile::*;
pub use defs::*;
use werbolg_core as ir;
use werbolg_core::{ConstrId, FunId, Ident, LitId, Span};
use werbolg_core::{ConstrId, FunId, Ident, LitId, Literal, Span};

use bindings::BindingsStack;
pub use environ::Environment;
Expand All @@ -27,6 +27,7 @@ pub enum CompilationError {
DuplicateSymbol(Ident),
MissingSymbol(Span, Ident),
FunctionParamsMoreThanLimit(usize),
LiteralNotSupported(Literal),
}

pub struct CompilationUnit<L> {
Expand Down
3 changes: 2 additions & 1 deletion werbolg-compile/src/params.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::CompilationError;
use werbolg_core::Literal;

pub struct CompilationParams<L: Clone + Eq + core::hash::Hash> {
/// Map a werbolg-literal into a L type that will be used during execution
pub literal_mapper: fn(Literal) -> L,
pub literal_mapper: fn(Literal) -> Result<L, CompilationError>,
}
144 changes: 1 addition & 143 deletions werbolg-exec/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,10 @@
//! Execution machine value - define the Value type
use super::{ExecutionError, ExecutionMachine};
/*
use alloc::rc::Rc;
use core::any::Any;
use core::cell::RefCell;

/*
/// Execution Machine Value
#[derive(Clone, Debug)]
pub enum Value {
Unit,
// Simple values
Small(u64),
Binary(Box<[u8]>),
Opaque(Opaque),
OpaqueMut(OpaqueMut),
// Composite
List(Box<[Value]>),
Struct(ConstrId, Box<[Value]>),
Enum(u32, Box<[Value]>),
// Functions
Fun(ValueFun),
}
#[derive(Debug, Clone)]
pub enum ValueKind {
Unit,
Small,
Binary,
Opaque,
OpaqueMut,
List,
Struct,
Enum,
Fun,
}
impl From<NifId> for Value {
fn from(value: NifId) -> Self {
Value::Fun(ValueFun::Native(value))
}
}
impl From<FunId> for Value {
fn from(value: FunId) -> Self {
Value::Fun(ValueFun::Fun(value))
}
}
impl<'a> From<&'a Value> for ValueKind {
fn from(value: &'a Value) -> Self {
match value {
Value::Unit => ValueKind::Unit,
Value::Small(_) => ValueKind::Small,
Value::Binary(_) => ValueKind::Binary,
Value::Opaque(_) => ValueKind::Opaque,
Value::OpaqueMut(_) => ValueKind::OpaqueMut,
Value::List(_) => ValueKind::List,
Value::Struct(_, _) => ValueKind::Struct,
Value::Enum(_, _) => ValueKind::Enum,
Value::Fun(_) => ValueKind::Fun,
}
}
}
*/

/// Native Implemented Function
Expand Down Expand Up @@ -137,88 +79,4 @@ impl core::fmt::Debug for OpaqueMut {
f.debug_tuple("OpaqueMut").field(&ty).finish()
}
}
/*
impl<'a> From<&'a Literal> for Value {
fn from(literal: &'a Literal) -> Value {
match literal {
Literal::Bool(b) =>
Literal::String(s) => Value::String(s.clone().into_boxed_str()),
Literal::Number(n) => Value::Number(n.clone()),
Literal::Decimal(d) => Value::Decimal(d.clone()),
Literal::Bytes(b) => Value::Bytes(b.clone()),
}
}
}
*/
impl Value {
pub fn make_opaque<T: Any + Send + Sync>(t: T) -> Self {
Value::Opaque(Opaque::new(t))
}
pub fn make_opaque_mut<T: Any + Send + Sync>(t: T) -> Self {
Value::OpaqueMut(OpaqueMut::new(t))
}
pub fn small<T: Any + Send + Sync>(&self) -> Result<u64, ExecutionError> {
match self {
Value::Small(o) => Ok(*o),
_ => Err(ExecutionError::ValueKindUnexpected {
value_expected: ValueKind::Small,
value_got: self.into(),
}),
}
}
pub fn binary<T: Any + Send + Sync>(&self) -> Result<Box<[u8]>, ExecutionError> {
match self {
Value::Binary(o) => Ok(*o),
_ => Err(ExecutionError::ValueKindUnexpected {
value_expected: ValueKind::Small,
value_got: self.into(),
}),
}
}
pub fn opaque<T: Any + Send + Sync>(&self) -> Result<&T, ExecutionError> {
match self {
Value::Opaque(o) => o.downcast_ref(),
_ => Err(ExecutionError::ValueKindUnexpected {
value_expected: ValueKind::Opaque,
value_got: self.into(),
}),
}
}
pub fn fun(&self) -> Result<ValueFun, ExecutionError> {
match self {
Value::Fun(valuefun) => Ok(*valuefun),
_ => Err(ExecutionError::ValueKindUnexpected {
value_expected: ValueKind::Fun,
value_got: self.into(),
}),
}
}
pub fn unit(&self) -> Result<(), ExecutionError> {
match self {
Value::Unit => Ok(()),
_ => Err(ExecutionError::ValueKindUnexpected {
value_expected: ValueKind::Unit,
value_got: self.into(),
}),
}
}
pub fn list(&self) -> Result<&[Value], ExecutionError> {
match self {
Value::List(v) => Ok(v),
_ => Err(ExecutionError::ValueKindUnexpected {
value_expected: ValueKind::List,
value_got: self.into(),
}),
}
}
}
*/

0 comments on commit 7cc2b13

Please sign in to comment.