Fix some instructions starting with digits being lexed as numbers

This commit is contained in:
Dominic Grimm 2023-03-31 15:13:01 +02:00
parent 804da393a8
commit 635ae21540
Signed by: dergrimm
GPG key ID: 12EFFCAEA9E620BF
8 changed files with 31 additions and 95 deletions

1
Cargo.lock generated
View file

@ -286,7 +286,6 @@ dependencies = [
"hence",
"indexmap",
"itertools",
"lazy_static",
"parse_int",
"snailquote",
]

View file

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

View file

@ -3,4 +3,4 @@
." test\n"
." jdsafhjfnjfn\n"
debug drop debug
test debug 2+ debug -0x50 debug

View file

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

View file

@ -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<T, U> {
fn compile(&self, data: &T) -> Result<U>;
}
@ -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::<Result<Vec<_>>>()?
// .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::<Result<Vec<_>>>()?
// .into_iter()
// .flatten(),
// );
// }
let conditions = self
.conditions
.iter()
@ -222,31 +173,6 @@ impl Compiler {
})
.collect::<Result<Vec<_>>>()?;
// 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::<Vec<_>>()
// {
// 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::<Result<Vec<hence::parser::ast::Body>>>()
// .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::<Result<Vec<_>>>()?;
// 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,

View file

@ -1250,7 +1250,6 @@ impl compiler::Compilable<compiler::Compiler, hence::parser::ast::Body> for Inst
// Instruction::Condition(x) => Ok(vec![]),
Instruction::Condition(x) => {
// dbg!(x);
Ok(vec![])
}

View file

@ -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<Vec<Token>> {
let mut chars = source.chars().peekable();
let mut tokens: Vec<Token> = vec![];
@ -46,7 +55,8 @@ pub fn lex(source: &str) -> Result<Vec<Token>> {
}
'\\' => 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<Vec<Token>> {
}
}
_ => Token::Word(x),
// _ => convert_literal(x),
}
}
});

View file

@ -41,7 +41,11 @@ pub fn parse(tokens: Vec<lexer::Token>) -> Result<ast::AST> {
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;