Move some other stuff to value as well
This commit is contained in:
parent
b1ddd31fb7
commit
139887fd73
@ -1,6 +1,7 @@
|
||||
use ferrite_lua::{
|
||||
compile, value,
|
||||
vm::{self, RuntimeError, RustFunction, VirtualMachine},
|
||||
compile,
|
||||
value::{self, RustFunction},
|
||||
vm::{self, RuntimeError, VirtualMachine},
|
||||
};
|
||||
|
||||
static TEST: &str = include_str!("../examples/test.lua");
|
||||
|
||||
@ -5,7 +5,7 @@ use crate::{
|
||||
Parse, TokenRange, TokenStream, TokenStreamError,
|
||||
lexer::{Keyword, Position, Token},
|
||||
},
|
||||
vm::{LuaFloat, LuaInteger},
|
||||
value::{LuaFloat, LuaInteger},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
||||
@ -8,7 +8,8 @@ use crate::{
|
||||
AccessModifier, BinaryOperator, Block, Expression, ExpressionList, IdentOrEllipsis,
|
||||
Literal, Node, Statement, UnaryOperator,
|
||||
},
|
||||
vm::{Constant, Instruction, LuaBool, LuaInteger, Prototype},
|
||||
value::{LuaBool, LuaInteger},
|
||||
vm::{Constant, Instruction, Prototype},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
//!
|
||||
//! #[derive(Debug, PartialEq, Eq)]
|
||||
//! pub struct Print;
|
||||
//! impl vm::RustFunction for Print {
|
||||
//! impl value::RustFunction for Print {
|
||||
//! fn execute(&self, parameters: Vec<value::Value>) -> Result<Vec<value::Value>, vm::RuntimeError> {
|
||||
//! println!("{:?}", parameters);
|
||||
//! Ok(Vec::new())
|
||||
|
||||
85
src/value.rs
85
src/value.rs
@ -2,9 +2,92 @@ use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
|
||||
|
||||
use crate::{
|
||||
ast::{BinaryOperator, UnaryOperator},
|
||||
vm::{Closure, LuaBool, LuaFloat, LuaInteger, RuntimeError, RustFunction, VMFloat},
|
||||
vm::{Closure, RuntimeError},
|
||||
};
|
||||
|
||||
#[derive(Clone, Hash, PartialEq, Eq, Copy, PartialOrd, Ord)]
|
||||
pub struct VMFloat(u64);
|
||||
|
||||
impl VMFloat {
|
||||
pub fn lua_number(&self) -> LuaFloat {
|
||||
LuaFloat(f64::from_bits(self.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for VMFloat {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.lua_number().fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct LuaFloat(pub f64);
|
||||
|
||||
impl LuaFloat {
|
||||
pub fn vm_number(&self) -> VMFloat {
|
||||
VMFloat(f64::to_bits(self.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for LuaFloat {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct LuaInteger(pub i64);
|
||||
|
||||
impl Debug for LuaInteger {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LuaInteger> for LuaFloat {
|
||||
fn from(value: LuaInteger) -> Self {
|
||||
LuaFloat(value.0 as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&LuaInteger> for LuaFloat {
|
||||
fn from(value: &LuaInteger) -> Self {
|
||||
LuaFloat(value.0 as f64)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct LuaBool(pub bool);
|
||||
|
||||
impl Debug for LuaBool {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LuaBool> for LuaInteger {
|
||||
fn from(value: LuaBool) -> Self {
|
||||
LuaInteger(value.0 as i64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&LuaBool> for LuaInteger {
|
||||
fn from(value: &LuaBool) -> Self {
|
||||
LuaInteger(value.0 as i64)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait RustFunction: Debug {
|
||||
fn execute(&self, parameters: Vec<Value>) -> Result<Vec<Value>, RuntimeError>;
|
||||
fn as_indexable(&self) -> String;
|
||||
}
|
||||
|
||||
impl<T: RustFunction + 'static> From<T> for Value {
|
||||
fn from(value: T) -> Self {
|
||||
Self::RustFunction(Rc::new(RefCell::new(value)))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Value {
|
||||
String(String),
|
||||
|
||||
85
src/vm.rs
85
src/vm.rs
@ -5,81 +5,9 @@ use std::{cell::RefCell, collections::HashMap, fmt::Debug, hash::Hash, rc::Rc};
|
||||
use crate::{
|
||||
CompilationUnit,
|
||||
ast::{BinaryOperator, UnaryOperator},
|
||||
value::{IndexableValue, Value},
|
||||
value::{IndexableValue, LuaBool, LuaInteger, VMFloat, Value},
|
||||
};
|
||||
|
||||
#[derive(Clone, Hash, PartialEq, Eq, Copy, PartialOrd, Ord)]
|
||||
pub struct VMFloat(u64);
|
||||
|
||||
impl VMFloat {
|
||||
pub fn lua_number(&self) -> LuaFloat {
|
||||
LuaFloat(f64::from_bits(self.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for VMFloat {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.lua_number().fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct LuaFloat(pub f64);
|
||||
|
||||
impl LuaFloat {
|
||||
pub fn vm_number(&self) -> VMFloat {
|
||||
VMFloat(f64::to_bits(self.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for LuaFloat {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct LuaInteger(pub i64);
|
||||
|
||||
impl Debug for LuaInteger {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LuaInteger> for LuaFloat {
|
||||
fn from(value: LuaInteger) -> Self {
|
||||
LuaFloat(value.0 as f64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&LuaInteger> for LuaFloat {
|
||||
fn from(value: &LuaInteger) -> Self {
|
||||
LuaFloat(value.0 as f64)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct LuaBool(pub bool);
|
||||
|
||||
impl Debug for LuaBool {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LuaBool> for LuaInteger {
|
||||
fn from(value: LuaBool) -> Self {
|
||||
LuaInteger(value.0 as i64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&LuaBool> for LuaInteger {
|
||||
fn from(value: &LuaBool) -> Self {
|
||||
LuaInteger(value.0 as i64)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Hash, PartialEq, Eq)]
|
||||
pub enum Constant {
|
||||
String(String),
|
||||
@ -270,17 +198,6 @@ impl Environment {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait RustFunction: Debug {
|
||||
fn execute(&self, parameters: Vec<Value>) -> Result<Vec<Value>, RuntimeError>;
|
||||
fn as_indexable(&self) -> String;
|
||||
}
|
||||
|
||||
impl<T: RustFunction + 'static> From<T> for Value {
|
||||
fn from(value: T) -> Self {
|
||||
Self::RustFunction(Rc::new(RefCell::new(value)))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct Prototype {
|
||||
pub instructions: Vec<Instruction>,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user