Fix some instructions starting with digits being lexed as numbers
This commit is contained in:
parent
804da393a8
commit
635ae21540
8 changed files with 31 additions and 95 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -286,7 +286,6 @@ dependencies = [
|
||||||
"hence",
|
"hence",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"itertools",
|
"itertools",
|
||||||
"lazy_static",
|
|
||||||
"parse_int",
|
"parse_int",
|
||||||
"snailquote",
|
"snailquote",
|
||||||
]
|
]
|
||||||
|
|
|
@ -131,7 +131,18 @@ return_call_stack_jump:
|
||||||
.std_alu CORE_ALU_GT
|
.std_alu CORE_ALU_GT
|
||||||
tlr CORE_REG_A
|
tlr CORE_REG_A
|
||||||
.std_cond_jump loop_strings_1
|
.std_cond_jump loop_strings_1
|
||||||
|
push 40
|
||||||
|
push 2
|
||||||
|
.stack_transfer_alu
|
||||||
|
.std_alu CORE_ALU_ADD
|
||||||
|
tls
|
||||||
dbg
|
dbg
|
||||||
pop
|
.std_ld
|
||||||
|
tlr CORE_REG_A
|
||||||
|
.std_rset CORE_REG_B, 2
|
||||||
|
.std_alu CORE_ALU_ADD
|
||||||
|
tls
|
||||||
|
dbg
|
||||||
|
push 65456
|
||||||
dbg
|
dbg
|
||||||
.std_stop
|
.std_stop
|
||||||
|
|
|
@ -3,4 +3,4 @@
|
||||||
." test\n"
|
." test\n"
|
||||||
." jdsafhjfnjfn\n"
|
." jdsafhjfnjfn\n"
|
||||||
|
|
||||||
debug drop debug
|
test debug 2+ debug -0x50 debug
|
||||||
|
|
|
@ -14,6 +14,5 @@ anyhow = { version = "1.0.62", features = ["backtrace"] }
|
||||||
itertools = "0.10.2"
|
itertools = "0.10.2"
|
||||||
parse_int = "0.6.0"
|
parse_int = "0.6.0"
|
||||||
indexmap = "1.9.1"
|
indexmap = "1.9.1"
|
||||||
lazy_static = "1.4.0"
|
|
||||||
askama = "0.12.0"
|
askama = "0.12.0"
|
||||||
snailquote = "0.3.1"
|
snailquote = "0.3.1"
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Result};
|
||||||
use indexmap::IndexSet;
|
use indexmap::IndexSet;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::parser;
|
use crate::parser;
|
||||||
|
@ -11,17 +10,6 @@ pub mod templates;
|
||||||
|
|
||||||
pub use instruction::Instruction;
|
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> {
|
pub trait Compilable<T, U> {
|
||||||
fn compile(&self, data: &T) -> Result<U>;
|
fn compile(&self, data: &T) -> Result<U>;
|
||||||
}
|
}
|
||||||
|
@ -157,44 +145,7 @@ impl Compiler {
|
||||||
data: s.to_owned(),
|
data: s.to_owned(),
|
||||||
})
|
})
|
||||||
.collect();
|
.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
|
let conditions = self
|
||||||
.conditions
|
.conditions
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -222,31 +173,6 @@ impl Compiler {
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>>>()?;
|
.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
|
let words = self
|
||||||
.words
|
.words
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -269,19 +195,6 @@ impl Compiler {
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>>>()?;
|
.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 {
|
Ok(templates::DefaultTemplate {
|
||||||
strings,
|
strings,
|
||||||
conditions,
|
conditions,
|
||||||
|
|
|
@ -1250,7 +1250,6 @@ impl compiler::Compilable<compiler::Compiler, hence::parser::ast::Body> for Inst
|
||||||
// Instruction::Condition(x) => Ok(vec![]),
|
// Instruction::Condition(x) => Ok(vec![]),
|
||||||
Instruction::Condition(x) => {
|
Instruction::Condition(x) => {
|
||||||
// dbg!(x);
|
// dbg!(x);
|
||||||
|
|
||||||
Ok(vec![])
|
Ok(vec![])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ use anyhow::Result;
|
||||||
use hence::assembler::ToCode;
|
use hence::assembler::ToCode;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
use crate::compiler;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Token {
|
pub enum Token {
|
||||||
Newline(usize),
|
Newline(usize),
|
||||||
|
@ -34,6 +36,13 @@ pub fn is_space(c: char) -> bool {
|
||||||
c.is_whitespace() || c == '\n'
|
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>> {
|
pub fn lex(source: &str) -> Result<Vec<Token>> {
|
||||||
let mut chars = source.chars().peekable();
|
let mut chars = source.chars().peekable();
|
||||||
let mut tokens: Vec<Token> = vec![];
|
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()),
|
'\\' => Token::BackslashComment(chars.peeking_take_while(|&c| c != '\n').collect()),
|
||||||
_ if c.is_numeric() => {
|
_ 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();
|
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),
|
_ => Token::Word(x),
|
||||||
|
// _ => convert_literal(x),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -41,7 +41,11 @@ pub fn parse(tokens: Vec<lexer::Token>) -> Result<ast::AST> {
|
||||||
string: snailquote::unescape(&format!("\"{}\"", string))?,
|
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() {
|
lexer::Token::Word(x) => match x.as_str() {
|
||||||
":" => {
|
":" => {
|
||||||
let mut depth: usize = 1;
|
let mut depth: usize = 1;
|
||||||
|
|
Loading…
Reference in a new issue