fix string add
This commit is contained in:
parent
774df709e2
commit
2fc6c449b3
6 changed files with 9 additions and 15 deletions
|
|
@ -85,6 +85,6 @@ impl Chunk {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_value(&self, value: &Value) {
|
fn print_value(&self, value: &Value) {
|
||||||
println!("'{:?}'", value);
|
println!("{:?}", value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use tracing::debug;
|
||||||
|
|
||||||
pub fn compile(source: &str) -> anyhow::Result<Chunk> {
|
pub fn compile(source: &str) -> anyhow::Result<Chunk> {
|
||||||
let tokens = scan(source);
|
let tokens = scan(source);
|
||||||
// println!("{:?}", tokens);
|
debug!("Scanned tokens: {:?}", tokens);
|
||||||
|
|
||||||
let mut compiler = Compiler {
|
let mut compiler = Compiler {
|
||||||
chunk: Chunk::new("main"),
|
chunk: Chunk::new("main"),
|
||||||
|
|
@ -77,7 +77,7 @@ impl<'a> Compiler<'a> {
|
||||||
fn parse_precedence(&mut self, precedence: usize) -> anyhow::Result<()> {
|
fn parse_precedence(&mut self, precedence: usize) -> anyhow::Result<()> {
|
||||||
self.advance()?;
|
self.advance()?;
|
||||||
let rule = get_rule(&self.previous_token.tokentype);
|
let rule = get_rule(&self.previous_token.tokentype);
|
||||||
debug!("{:?}",rule);
|
debug!("Precedence rule: {:?}",rule);
|
||||||
if let Some(prefix) = rule.prefix {
|
if let Some(prefix) = rule.prefix {
|
||||||
prefix(self)?;
|
prefix(self)?;
|
||||||
while precedence <= get_rule(&self.current_token.tokentype).precedence {
|
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 {
|
match s.previous_token.tokentype {
|
||||||
TokenType::False => s.emit_byte(OP_FALSE),
|
TokenType::False => s.emit_byte(OP_FALSE),
|
||||||
TokenType::True => s.emit_byte(OP_TRUE),
|
TokenType::True => s.emit_byte(OP_TRUE),
|
||||||
|
TokenType::String => s.emit_constant(Value::String(s.previous_token.lexeme.clone())),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -170,7 +171,7 @@ fn unary(s: &mut Compiler) -> anyhow::Result<()> {
|
||||||
|
|
||||||
fn binary(s: &mut Compiler) -> anyhow::Result<()> {
|
fn binary(s: &mut Compiler) -> anyhow::Result<()> {
|
||||||
let operator_type = &s.previous_token.tokentype;
|
let operator_type = &s.previous_token.tokentype;
|
||||||
debug!("{:?}",operator_type);
|
debug!("operator {:?}",operator_type);
|
||||||
let rule = get_rule(operator_type);
|
let rule = get_rule(operator_type);
|
||||||
s.parse_precedence(rule.precedence + 1)?;
|
s.parse_precedence(rule.precedence + 1)?;
|
||||||
match operator_type {
|
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::LessEqual, Rule::new(None, None, PREC_NONE));
|
||||||
rules.insert(TokenType::LessLess, Rule::new(None, Some(binary), PREC_BITSHIFT));
|
rules.insert(TokenType::LessLess, Rule::new(None, Some(binary), PREC_BITSHIFT));
|
||||||
rules.insert(TokenType::Identifier, Rule::new(None, None, PREC_NONE));
|
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::Number, Rule::new(Some(number), None, PREC_NONE));
|
||||||
rules.insert(TokenType::LogicalAnd, Rule::new(None, Some(binary), PREC_AND));
|
rules.insert(TokenType::LogicalAnd, Rule::new(None, Some(binary), PREC_AND));
|
||||||
rules.insert(TokenType::LogicalOr, Rule::new(None, Some(binary), PREC_OR));
|
rules.insert(TokenType::LogicalOr, Rule::new(None, Some(binary), PREC_OR));
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,6 @@
|
||||||
use crate::chunk::Chunk;
|
|
||||||
use crate::value::Value;
|
|
||||||
use anyhow::anyhow;
|
|
||||||
use tracing::debug;
|
|
||||||
|
|
||||||
pub mod chunk;
|
pub mod chunk;
|
||||||
pub mod compiler;
|
pub mod compiler;
|
||||||
mod keywords;
|
mod keywords;
|
||||||
pub mod opcode;
|
|
||||||
pub mod scanner;
|
pub mod scanner;
|
||||||
mod tokens;
|
mod tokens;
|
||||||
mod value;
|
mod value;
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
let chunk = crudlang::compiler::compile("3<<2")?;
|
let chunk = crudlang::compiler::compile("\"a\"+\"b\"")?;
|
||||||
chunk.disassemble();
|
chunk.disassemble();
|
||||||
|
|
||||||
let result = crudlang::vm::interpret(chunk);
|
let result = crudlang::vm::interpret(chunk);
|
||||||
|
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
|
|
||||||
|
|
@ -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::F64(f)) => Ok(Value::String(format!("{}{}", s, f))),
|
||||||
(Value::String(s), Value::Bool(b)) => Ok(Value::String(format!("{}{}", s, b))),
|
(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(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::List(l)) => Ok(Value::String(format!("{}{:?}", s1, l))),
|
||||||
(Value::String(s1), Value::Map(m)) => Ok(Value::String(format!("{}{:?}", s1, m))),
|
(Value::String(s1), Value::Map(m)) => Ok(Value::String(format!("{}{:?}", s1, m))),
|
||||||
//enum?
|
//enum?
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue