diff --git a/src/chunk.rs b/src/chunk.rs index d88d7fa..8b4bf06 100644 --- a/src/chunk.rs +++ b/src/chunk.rs @@ -85,6 +85,6 @@ impl Chunk { } fn print_value(&self, value: &Value) { - println!("'{:?}'", value); + println!("{:?}", value); } } diff --git a/src/compiler.rs b/src/compiler.rs index 45e430b..3e5582d 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -10,7 +10,7 @@ use tracing::debug; pub fn compile(source: &str) -> anyhow::Result { let tokens = scan(source); - // println!("{:?}", tokens); + debug!("Scanned tokens: {:?}", tokens); let mut compiler = Compiler { chunk: Chunk::new("main"), @@ -77,7 +77,7 @@ impl<'a> Compiler<'a> { fn parse_precedence(&mut self, precedence: usize) -> anyhow::Result<()> { self.advance()?; let rule = get_rule(&self.previous_token.tokentype); - debug!("{:?}",rule); + debug!("Precedence rule: {:?}",rule); if let Some(prefix) = rule.prefix { prefix(self)?; while precedence <= get_rule(&self.current_token.tokentype).precedence { @@ -141,6 +141,7 @@ fn literal(s: &mut Compiler) -> anyhow::Result<()> { match s.previous_token.tokentype { TokenType::False => s.emit_byte(OP_FALSE), TokenType::True => s.emit_byte(OP_TRUE), + TokenType::String => s.emit_constant(Value::String(s.previous_token.lexeme.clone())), _ => {} } Ok(()) @@ -170,7 +171,7 @@ fn unary(s: &mut Compiler) -> anyhow::Result<()> { fn binary(s: &mut Compiler) -> anyhow::Result<()> { let operator_type = &s.previous_token.tokentype; - debug!("{:?}",operator_type); + debug!("operator {:?}",operator_type); let rule = get_rule(operator_type); s.parse_precedence(rule.precedence + 1)?; match operator_type { @@ -225,7 +226,7 @@ static RULES: LazyLock> = LazyLock::new(|| { rules.insert(TokenType::LessEqual, Rule::new(None, None, PREC_NONE)); rules.insert(TokenType::LessLess, Rule::new(None, Some(binary), PREC_BITSHIFT)); rules.insert(TokenType::Identifier, Rule::new(None, None, PREC_NONE)); - rules.insert(TokenType::String, Rule::new(None, None, PREC_NONE)); + rules.insert(TokenType::String, Rule::new(Some(literal), None, PREC_NONE)); rules.insert(TokenType::Number, Rule::new(Some(number), None, PREC_NONE)); rules.insert(TokenType::LogicalAnd, Rule::new(None, Some(binary), PREC_AND)); rules.insert(TokenType::LogicalOr, Rule::new(None, Some(binary), PREC_OR)); diff --git a/src/lib.rs b/src/lib.rs index c14de4d..c31ebf8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,6 @@ -use crate::chunk::Chunk; -use crate::value::Value; -use anyhow::anyhow; -use tracing::debug; - pub mod chunk; pub mod compiler; mod keywords; -pub mod opcode; pub mod scanner; mod tokens; mod value; diff --git a/src/main.rs b/src/main.rs index 55b879a..81e860e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ fn main() -> anyhow::Result<()> { tracing_subscriber::fmt::init(); - - let chunk = crudlang::compiler::compile("3<<2")?; + + let chunk = crudlang::compiler::compile("\"a\"+\"b\"")?; chunk.disassemble(); let result = crudlang::vm::interpret(chunk); diff --git a/src/opcode.rs b/src/opcode.rs deleted file mode 100644 index 8b13789..0000000 --- a/src/opcode.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/value.rs b/src/value.rs index 0b56c67..85ab4b2 100644 --- a/src/value.rs +++ b/src/value.rs @@ -128,7 +128,7 @@ impl Add<&Value> for &Value { (Value::String(s), Value::F64(f)) => Ok(Value::String(format!("{}{}", s, f))), (Value::String(s), Value::Bool(b)) => Ok(Value::String(format!("{}{}", s, b))), (Value::String(s), Value::Char(c)) => Ok(Value::String(format!("{}{}", s, c))), - (Value::String(s1), Value::String(s2)) => Ok(Value::String(format!("{}{}", s1, s2))), + (Value::String(s1), Value::String(s2)) => Ok(Value::String(format!("{}{}", s2, s1))), (Value::String(s1), Value::List(l)) => Ok(Value::String(format!("{}{:?}", s1, l))), (Value::String(s1), Value::Map(m)) => Ok(Value::String(format!("{}{:?}", s1, m))), //enum?