Add String-values and String-literals
This commit is contained in:
parent
093e96f8b2
commit
d4da1f9184
@ -21,4 +21,5 @@ print(add(10)(15))
|
||||
print(add(10)(15))
|
||||
print(b)
|
||||
print(min(11, 9))
|
||||
print(10 - 15)
|
||||
print(10 - 15)
|
||||
print("hello there!")
|
||||
@ -421,6 +421,9 @@ impl Parse for PrimaryExpression {
|
||||
Expression::Literal(Literal::Number(
|
||||
u64::from_str_radix(&value, 10).unwrap() as f64
|
||||
))
|
||||
} else if let Some(Token::StringLit(value)) = peeked {
|
||||
stream.next(); // Consume string-literal
|
||||
Expression::Literal(Literal::String(value))
|
||||
} else {
|
||||
Expression::ValueRef(stream.parse()?)
|
||||
};
|
||||
@ -492,7 +495,8 @@ fn parse_binop_rhs(
|
||||
Ok(lhs.0)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Literal {
|
||||
Number(LuaNumber),
|
||||
String(String),
|
||||
}
|
||||
|
||||
@ -318,6 +318,11 @@ impl Expression {
|
||||
constants.insert(Constant::Number(value.to_bits()));
|
||||
constants
|
||||
}
|
||||
Literal::String(value) => {
|
||||
let mut constants = HashSet::new();
|
||||
constants.insert(Constant::String(value.clone()));
|
||||
constants
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -530,6 +535,7 @@ impl Expression {
|
||||
reg,
|
||||
state.get_constant(&match literal {
|
||||
Literal::Number(value) => Constant::Number(value.to_bits()),
|
||||
Literal::String(value) => Constant::String(value.clone()),
|
||||
}),
|
||||
));
|
||||
(instructions, vec![reg])
|
||||
|
||||
28
src/main.rs
28
src/main.rs
@ -104,22 +104,22 @@ fn main() {
|
||||
let mut run = closure.run(Vec::new());
|
||||
|
||||
while run.next().unwrap().is_none() {
|
||||
let (instructions, state, constants) =
|
||||
compile("print(b)", constants.clone(), state.prototypes.clone());
|
||||
// let (instructions, state, constants) =
|
||||
// compile("print(b)", constants.clone(), state.prototypes.clone());
|
||||
|
||||
// dbg!(&instructions);
|
||||
// // dbg!(&instructions);
|
||||
|
||||
let mut new_run = run.execute(instructions, state, constants);
|
||||
while {
|
||||
match new_run.next() {
|
||||
Ok(Some(_)) => false,
|
||||
Ok(None) => true,
|
||||
Err(e) => {
|
||||
print!("Error: {}", e);
|
||||
false
|
||||
}
|
||||
}
|
||||
} {}
|
||||
// let mut new_run = run.execute(instructions, state, constants);
|
||||
// while {
|
||||
// match new_run.next() {
|
||||
// Ok(Some(_)) => false,
|
||||
// Ok(None) => true,
|
||||
// Err(e) => {
|
||||
// print!("Error: {}", e);
|
||||
// false
|
||||
// }
|
||||
// }
|
||||
// } {}
|
||||
}
|
||||
|
||||
dbg!(&vm.environment.borrow().globals);
|
||||
|
||||
12
src/vm.rs
12
src/vm.rs
@ -122,6 +122,7 @@ pub struct Environment {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Value {
|
||||
String(String),
|
||||
Number(VMNumber),
|
||||
RustFunction(Rc<RefCell<dyn RustFunction>>),
|
||||
Function(Closure),
|
||||
@ -236,15 +237,16 @@ impl Value {
|
||||
impl Debug for Value {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Number(arg0) => f
|
||||
Value::Number(arg0) => f
|
||||
.debug_tuple("Number")
|
||||
.field(&LuaNumber::from_bits(*arg0))
|
||||
.finish(),
|
||||
Self::RustFunction(arg0) => f.debug_tuple("RustFunction").field(arg0).finish(),
|
||||
Self::Function(closure) => f
|
||||
Value::String(value) => f.debug_tuple("String").field(value).finish(),
|
||||
Value::RustFunction(arg0) => f.debug_tuple("RustFunction").field(arg0).finish(),
|
||||
Value::Function(closure) => f
|
||||
.debug_tuple(&format!("Function({})", closure.prototype))
|
||||
.finish(),
|
||||
Self::Nil => write!(f, "Nil"),
|
||||
Value::Nil => write!(f, "Nil"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -420,7 +422,7 @@ impl ClosureRunner {
|
||||
self.set_stack(
|
||||
*reg,
|
||||
match constants.get(*constant as usize).unwrap() {
|
||||
Constant::String(_) => todo!(),
|
||||
Constant::String(value) => Value::String(value.clone()),
|
||||
Constant::Number(value) => Value::Number(*value),
|
||||
},
|
||||
);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user