fix string add

This commit is contained in:
Shautvast 2025-10-20 20:59:50 +02:00
parent 774df709e2
commit 2fc6c449b3
6 changed files with 9 additions and 15 deletions

View file

@ -85,6 +85,6 @@ impl Chunk {
}
fn print_value(&self, value: &Value) {
println!("'{:?}'", value);
println!("{:?}", value);
}
}

View file

@ -10,7 +10,7 @@ use tracing::debug;
pub fn compile(source: &str) -> anyhow::Result<Chunk> {
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<HashMap<TokenType, Rule>> = 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));

View file

@ -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;

View file

@ -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);

View file

@ -1 +0,0 @@

View file

@ -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?