From 635ae21540503e3ec251e62c9c656cc83bef4e8b Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Fri, 31 Mar 2023 15:13:01 +0200 Subject: [PATCH] Fix some instructions starting with digits being lexed as numbers --- Cargo.lock | 1 - examples/testforth.asm | 13 +++- examples/testforth.fth | 2 +- henceforth/Cargo.toml | 1 - henceforth/src/compiler.rs | 89 +------------------------- henceforth/src/compiler/instruction.rs | 1 - henceforth/src/lexer.rs | 13 +++- henceforth/src/parser.rs | 6 +- 8 files changed, 31 insertions(+), 95 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 629fa85..6f9a8e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -286,7 +286,6 @@ dependencies = [ "hence", "indexmap", "itertools", - "lazy_static", "parse_int", "snailquote", ] diff --git a/examples/testforth.asm b/examples/testforth.asm index 8a25fa2..2c217fe 100644 --- a/examples/testforth.asm +++ b/examples/testforth.asm @@ -131,7 +131,18 @@ return_call_stack_jump: .std_alu CORE_ALU_GT tlr CORE_REG_A .std_cond_jump loop_strings_1 + push 40 + push 2 + .stack_transfer_alu + .std_alu CORE_ALU_ADD + tls dbg - pop + .std_ld + tlr CORE_REG_A + .std_rset CORE_REG_B, 2 + .std_alu CORE_ALU_ADD + tls + dbg + push 65456 dbg .std_stop diff --git a/examples/testforth.fth b/examples/testforth.fth index 4f7c413..b37e53e 100644 --- a/examples/testforth.fth +++ b/examples/testforth.fth @@ -3,4 +3,4 @@ ." test\n" ." jdsafhjfnjfn\n" -debug drop debug +test debug 2+ debug -0x50 debug diff --git a/henceforth/Cargo.toml b/henceforth/Cargo.toml index 0540341..f5baa60 100644 --- a/henceforth/Cargo.toml +++ b/henceforth/Cargo.toml @@ -14,6 +14,5 @@ anyhow = { version = "1.0.62", features = ["backtrace"] } itertools = "0.10.2" parse_int = "0.6.0" indexmap = "1.9.1" -lazy_static = "1.4.0" askama = "0.12.0" snailquote = "0.3.1" diff --git a/henceforth/src/compiler.rs b/henceforth/src/compiler.rs index 0855955..686d75a 100644 --- a/henceforth/src/compiler.rs +++ b/henceforth/src/compiler.rs @@ -1,7 +1,6 @@ -use anyhow::{bail, Context, Result}; +use anyhow::{bail, Result}; use indexmap::IndexSet; use itertools::Itertools; -use lazy_static::lazy_static; use std::collections::HashMap; use crate::parser; @@ -11,17 +10,6 @@ pub mod templates; pub use instruction::Instruction; -// pub const TEMPLATE_ASM: &str = include_str!("compiler/templates/default.asm"); - -// lazy_static! { -// #[derive(Debug)] -// pub static ref TEMPLATE: hence::parser::ast::Body = hence::parser::parse( -// hence::lexer::lex(TEMPLATE_ASM).unwrap() -// ) -// .unwrap() -// .body; -// } - pub trait Compilable { fn compile(&self, data: &T) -> Result; } @@ -157,44 +145,7 @@ impl Compiler { data: s.to_owned(), }) .collect(); - dbg!(&self.strings); - // for (id, s) in self.strings.iter().enumerate() { - // x.extend([ - // hence::parser::ast::Node::Label(format!("data_strings_{}", id)), - // hence::parser::ast::Node::MacroCall { - // name: "bytes".to_string(), - // args: vec![hence::arg::Arg::String(s.to_string())], - // }, - // hence::parser::ast::Node::Label(format!("data_strings_end_{}", id)), - // ]); - // } - // for (id, c) in self.conditions.iter().enumerate() { - // x.push(hence::parser::ast::Node::Label(format!( - // "conditions_if_{}", - // id - // ))); - // x.extend( - // c.if_instructions - // .iter() - // .map(|ins| ins.compile(self)) - // .collect::>>()? - // .into_iter() - // .flatten(), - // ); - // x.push(hence::parser::ast::Node::Label(format!( - // "conditions_else_{}", - // id - // ))); - // x.extend( - // c.else_instructions - // .iter() - // .map(|ins| ins.compile(self)) - // .collect::>>()? - // .into_iter() - // .flatten(), - // ); - // } let conditions = self .conditions .iter() @@ -222,31 +173,6 @@ impl Compiler { }) .collect::>>()?; - // for (name, word) in &self - // .words - // .iter() - // .filter(|(_, w)| w.times_used > 1) - // .sorted_by(|a, b| Ord::cmp(&a.1.id, &b.1.id)) - // .collect::>() - // { - // x.extend(vec![ - // hence::parser::ast::Node::Label(format!("words_{}", word.id)), - // hence::parser::ast::Node::Comment(format!("word: \"{}\"", name)), - // ]); - // x.extend( - // word.instructions - // .iter() - // .map(|ins| ins.compile(self)) - // .collect::>>() - // .unwrap() - // .into_iter() - // .flatten(), - // ); - // x.push(hence::parser::ast::Node::MacroCall { - // name: "return_call_stack_jump".to_string(), - // args: vec![], - // }); - // } let words = self .words .iter() @@ -269,19 +195,6 @@ impl Compiler { }) .collect::>>()?; - // x.extend([ - // hence::parser::ast::Node::Label("main".to_string()), - // hence::parser::ast::Node::MacroCall { - // name: "main".to_string(), - // args: vec![hence::arg::Arg::Variable("main".to_string())], - // }, - // ]); - // x.extend(body); - // x.push(hence::parser::ast::Node::MacroCall { - // name: "std_stop".to_string(), - // args: vec![], - // }); - Ok(templates::DefaultTemplate { strings, conditions, diff --git a/henceforth/src/compiler/instruction.rs b/henceforth/src/compiler/instruction.rs index 70b6099..498f31d 100644 --- a/henceforth/src/compiler/instruction.rs +++ b/henceforth/src/compiler/instruction.rs @@ -1250,7 +1250,6 @@ impl compiler::Compilable for Inst // Instruction::Condition(x) => Ok(vec![]), Instruction::Condition(x) => { // dbg!(x); - Ok(vec![]) } diff --git a/henceforth/src/lexer.rs b/henceforth/src/lexer.rs index edc3e83..d5f8549 100644 --- a/henceforth/src/lexer.rs +++ b/henceforth/src/lexer.rs @@ -2,6 +2,8 @@ use anyhow::Result; use hence::assembler::ToCode; use itertools::Itertools; +use crate::compiler; + #[derive(Debug)] pub enum Token { Newline(usize), @@ -34,6 +36,13 @@ pub fn is_space(c: char) -> bool { c.is_whitespace() || c == '\n' } +pub fn convert_to_word_with_number_bias(s: String) -> Token { + match compiler::Instruction::from_word(&s) { + Some(_) => Token::Word(s), + None => Token::Number(s), + } +} + pub fn lex(source: &str) -> Result> { let mut chars = source.chars().peekable(); let mut tokens: Vec = vec![]; @@ -46,7 +55,8 @@ pub fn lex(source: &str) -> Result> { } '\\' => Token::BackslashComment(chars.peeking_take_while(|&c| c != '\n').collect()), _ if c.is_numeric() => { - Token::Number(chars.peeking_take_while(|&c| !is_space(c)).collect()) + // Token::Number(chars.peeking_take_while(|&c| !is_space(c)).collect()) + convert_to_word_with_number_bias(chars.peeking_take_while(|&c| !is_space(c)).collect()) } _ => { let x: String = chars.peeking_take_while(|&c| !is_space(c)).collect(); @@ -75,6 +85,7 @@ pub fn lex(source: &str) -> Result> { } } _ => Token::Word(x), + // _ => convert_literal(x), } } }); diff --git a/henceforth/src/parser.rs b/henceforth/src/parser.rs index aa32c1d..db79e0f 100644 --- a/henceforth/src/parser.rs +++ b/henceforth/src/parser.rs @@ -41,7 +41,11 @@ pub fn parse(tokens: Vec) -> Result { string: snailquote::unescape(&format!("\"{}\"", string))?, }); } - lexer::Token::Number(x) => body.push(ast::Node::Number(parse_int::parse(&x)?)), + lexer::Token::Number(x) => body.push(ast::Node::Number(if x.starts_with('-') { + -parse_int::parse(&x[1..])? + } else { + parse_int::parse(&x)? + })), lexer::Token::Word(x) => match x.as_str() { ":" => { let mut depth: usize = 1;