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