Add language support and massively update Forth compiler

This commit is contained in:
Dominic Grimm 2023-04-01 19:20:32 +02:00
parent 635ae21540
commit df73353bc6
No known key found for this signature in database
GPG key ID: 12EFFCAEA9E620BF
31 changed files with 2217 additions and 331 deletions

View file

@ -27,6 +27,12 @@ pub struct Condition {
pub else_instructions: Vec<Instruction>,
}
impl Condition {
pub fn only_if(&self) -> bool {
self.else_instructions.is_empty()
}
}
#[derive(Debug)]
pub enum CallableId {
Word(String),
@ -206,13 +212,13 @@ impl Compiler {
}
pub fn compile(ast: parser::ast::AST, optimize: bool) -> Result<String> {
let mut data = Compiler::default();
let instructions = data.generate_instructions(ast.body, optimize)?;
let mut compiler = Compiler::default();
let instructions = compiler.generate_instructions(ast.body, optimize)?;
Ok(data.embed(
Ok(compiler.embed(
instructions
.iter()
.map(|ins| ins.compile(&data))
.map(|ins| ins.compile(&compiler))
.collect::<Result<Vec<hence::parser::ast::Body>>>()?
.into_iter()
.flatten()

View file

@ -76,7 +76,7 @@ pub enum Instruction {
}
impl compiler::Compilable<compiler::Compiler, hence::parser::ast::Body> for Instruction {
fn compile(&self, data: &compiler::Compiler) -> Result<hence::parser::ast::Body> {
fn compile(&self, compiler: &compiler::Compiler) -> Result<hence::parser::ast::Body> {
match self {
Instruction::Nop => Ok(vec![hence::parser::ast::Node::Call {
name: "nop".to_string(),
@ -1223,7 +1223,7 @@ impl compiler::Compilable<compiler::Compiler, hence::parser::ast::Body> for Inst
},
]),
Instruction::Mod => Ok(vec![]),
Instruction::Call(x) => match data.words.get(x) {
Instruction::Call(x) => match compiler.words.get(x) {
Some(w) => {
if w.times_used > 1 {
Ok(vec![hence::parser::ast::Node::MacroCall {
@ -1236,7 +1236,7 @@ impl compiler::Compilable<compiler::Compiler, hence::parser::ast::Body> for Inst
} else {
Ok(w.instructions
.iter()
.map(|ins| ins.compile(data))
.map(|ins| ins.compile(compiler))
.collect::<Result<Vec<hence::parser::ast::Body>>>()?
.into_iter()
.flatten()
@ -1248,22 +1248,39 @@ impl compiler::Compilable<compiler::Compiler, hence::parser::ast::Body> for Inst
// Instruction::Call(x) => bail!("Unknown word: {}", x),
Instruction::AsmQuote(x) => Ok(hence::parser::parse(hence::lexer::lex(x)?)?.body),
// Instruction::Condition(x) => Ok(vec![]),
Instruction::Condition(x) => {
// dbg!(x);
Ok(vec![])
}
Instruction::Condition(x) => match compiler.conditions.get(*x) {
Some(cond) => Ok(vec![if cond.only_if() {
hence::parser::ast::Node::MacroCall {
name: "call_stack_jump_cond_if".to_string(),
args: vec![
hence::arg::Arg::Variable(format!("conditions_if_{}", x)),
hence::arg::Arg::Variable("OFFSET".to_string()),
],
}
} else {
hence::parser::ast::Node::MacroCall {
name: "call_stack_jump_cond_if_else".to_string(),
args: vec![
hence::arg::Arg::Variable(format!("conditions_if_{}", x)),
hence::arg::Arg::Variable(format!("conditions_else_{}", x)),
hence::arg::Arg::Variable("OFFSET".to_string()),
],
}
}]),
None => bail!("Condition by index not found: {}", x),
},
Instruction::Multiple { instruction, count } => {
if *count == 0 {
Ok(vec![])
} else if *count == 1 {
Ok(instruction.compile(data)?)
Ok(instruction.compile(compiler)?)
} else {
match **instruction {
Instruction::Nop => Ok(Instruction::Nop.compile(data)?),
Instruction::Quit => Ok(Instruction::Quit.compile(data)?),
Instruction::Nop => Ok(Instruction::Nop.compile(compiler)?),
Instruction::Quit => Ok(Instruction::Quit.compile(compiler)?),
Instruction::Push(x) => Ok([
Instruction::Push(x).compile(data)?,
Instruction::Push(x).compile(compiler)?,
[hence::parser::ast::Node::Call {
name: "tls".to_string(),
arg: None,
@ -1275,16 +1292,16 @@ impl compiler::Compilable<compiler::Compiler, hence::parser::ast::Body> for Inst
]
.concat()),
Instruction::Depth => Ok([
Instruction::Depth.compile(data)?,
Instruction::Depth.compile(compiler)?,
Instruction::Multiple {
instruction: Box::new(Instruction::Dup),
count: count - 1,
}
.compile(data)?,
.compile(compiler)?,
]
.concat()),
Instruction::Dup => Ok([
Instruction::Dup.compile(data)?,
Instruction::Dup.compile(compiler)?,
[hence::parser::ast::Node::Call {
name: "tls".to_string(),
arg: None,
@ -1299,11 +1316,11 @@ impl compiler::Compilable<compiler::Compiler, hence::parser::ast::Body> for Inst
if count % 2 == 0 {
Ok(vec![])
} else {
Ok(Instruction::Swap.compile(data)?)
Ok(Instruction::Swap.compile(compiler)?)
}
}
Instruction::Over => Ok([
Instruction::Over.compile(data)?,
Instruction::Over.compile(compiler)?,
(0..count - 1)
.flat_map(|n| {
if n % 2 == 0 {
@ -1339,7 +1356,7 @@ impl compiler::Compilable<compiler::Compiler, hence::parser::ast::Body> for Inst
.concat()),
Instruction::Rot => match count % 3 {
0 => Ok(vec![]),
1 => Ok(Instruction::Rot.compile(data)?),
1 => Ok(Instruction::Rot.compile(compiler)?),
2 => Ok(vec![
hence::parser::ast::Node::MacroCall {
name: "std_ld".to_string(),
@ -1412,7 +1429,7 @@ impl compiler::Compilable<compiler::Compiler, hence::parser::ast::Body> for Inst
]
.concat()),
Instruction::I | Instruction::J => Ok([
instruction.compile(data)?,
instruction.compile(compiler)?,
[hence::parser::ast::Node::Call {
name: "tls".to_string(),
arg: None,
@ -1443,7 +1460,7 @@ impl compiler::Compilable<compiler::Compiler, hence::parser::ast::Body> for Inst
]
.concat()),
Instruction::Space | Instruction::Cr => Ok([
instruction.compile(data)?,
instruction.compile(compiler)?,
[hence::parser::ast::Node::Call {
name: "set".to_string(),
arg: None,
@ -1455,7 +1472,7 @@ impl compiler::Compilable<compiler::Compiler, hence::parser::ast::Body> for Inst
]
.concat()),
_ => {
let compiled = instruction.compile(data)?;
let compiled = instruction.compile(compiler)?;
let len = compiled.len() * count;
Ok(compiled.into_iter().cycle().take(len).collect())
}

View file

@ -1,5 +1,4 @@
use askama::Template;
use hence::assembler::ToCode;
pub struct IdLike<T, U> {
pub id: T,

View file

@ -1,6 +1,7 @@
use anyhow::Result;
use hence::assembler::ToCode;
use itertools::Itertools;
use std::fmt;
use crate::compiler;
@ -18,20 +19,22 @@ pub enum Token {
Word(String),
}
impl ToCode for Token {
fn to_code(&self) -> String {
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Token::Newline(x) => ["\n"].into_iter().cycle().take(*x).join(""),
Token::Whitespace(x) => [" "].into_iter().cycle().take(*x).join(""),
Token::ParenComment(x) => format!("( {})", x),
Token::BackslashComment(x) => format!("\\{}", x),
Token::DoubleDashComment(x) => format!("-- {}", x),
Token::StringLiteral { mode, string } => format!("{}\" {}\"", mode, string),
Token::Number(x) | Token::Word(x) => x.clone(),
Token::Newline(x) => write!(f, "{}", ["\n"].into_iter().cycle().take(*x).join("")),
Token::Whitespace(x) => write!(f, "{}", [" "].into_iter().cycle().take(*x).join("")),
Token::ParenComment(x) => write!(f, "( {})", x),
Token::BackslashComment(x) => write!(f, "\\{}", x),
Token::DoubleDashComment(x) => write!(f, "-- {}", x),
Token::StringLiteral { mode, string } => write!(f, "{}\" {}\"", mode, string),
Token::Number(x) | Token::Word(x) => write!(f, "{}", x),
}
}
}
impl ToCode for Token {}
pub fn is_space(c: char) -> bool {
c.is_whitespace() || c == '\n'
}
@ -56,7 +59,9 @@ 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())
convert_to_word_with_number_bias(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();

View file

@ -1,5 +1,5 @@
use hence::assembler::ToCode;
use itertools::Itertools;
use std::fmt;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct StackResult {
@ -7,9 +7,10 @@ pub struct StackResult {
pub after: Vec<String>,
}
impl ToCode for StackResult {
fn to_code(&self) -> String {
format!(
impl fmt::Display for StackResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}--{}",
if self.before.is_empty() {
"".to_string()
@ -25,6 +26,8 @@ impl ToCode for StackResult {
}
}
impl ToCode for StackResult {}
#[derive(Debug, PartialEq, Eq)]
pub enum Node {
Comment(String),
@ -45,37 +48,49 @@ pub enum Node {
Word(String),
}
impl ToCode for Node {
fn to_code(&self) -> String {
impl fmt::Display for Node {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Node::Comment(x) => format!("\\ {}", x),
Node::String { mode, string } => format!("{}\" {}\"", mode, string),
Node::Number(x) => x.to_string(),
Node::WordDefinition { name, stack, body } => format!(
Node::Comment(x) => write!(f, "\\ {}", x),
Node::String { mode, string } => write!(f, "{}\" {}\"", mode, string),
Node::Number(x) => write!(f, "{}", x),
Node::WordDefinition { name, stack, body } => write!(
f,
": {}{} {} ;",
name,
match stack {
Some(x) => format!(" {}", x.to_code()),
Some(x) => format!(" {}", x),
None => "".to_string(),
},
body.iter().map(|x| x.to_code()).join(" ")
// body.iter().map(|x| x.to_code()).join(" ")
itertools::free::join(body, " ")
),
Node::Condition { if_body, else_body } => {
if else_body.is_empty() {
format!("if {} then", if_body.iter().map(|x| x.to_code()).join(" "))
write!(
f,
"if {} then",
// if_body.iter().map(|x| x.to_code()).join(" ")
itertools::free::join(if_body, " ")
)
} else {
format!(
write!(
f,
"if {} else {} then",
if_body.iter().map(|x| x.to_code()).join(" "),
else_body.iter().map(|x| x.to_code()).join(" ")
// if_body.iter().map(|x| x.to_code()).join(" "),
itertools::free::join(if_body, " "),
// else_body.iter().map(|x| x.to_code()).join(" ")
itertools::free::join(else_body, " "),
)
}
}
Node::Word(x) => x.to_owned(),
Node::Word(x) => write!(f, "{}", x),
}
}
}
impl ToCode for Node {}
pub type Body = Vec<Node>;
#[derive(Debug)]
@ -83,8 +98,10 @@ pub struct AST {
pub body: Body,
}
impl ToCode for AST {
fn to_code(&self) -> String {
self.body.iter().map(|x| x.to_code()).join(" ")
impl fmt::Display for AST {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", itertools::free::join(&self.body, "\n"))
}
}
impl ToCode for AST {}

View file

@ -4,24 +4,42 @@
.include "$lib/core.asm"
.include "$lib/std.asm"
.include "$lib/main.asm"
;.include "$lib/main.asm"
.define MEM_LOOP_I, CORE_MEM_MEM
.define MEM_LOOP_J, (MEM_LOOP_I + 1)
.define_eval prev, OFFSET
ts CORE_U16_MAX
tlr CORE_REG_PC
.define_eval diff, (OFFSET - prev)
.define_eval MAIN_JUMPER, (CORE_MEM_PRG_END - diff)
.org prev
.delete prev, diff
.define MEM_CALL_STACK_LEN, 16
.define MEM_CALL_STACK_PTR, (MEM_LOOP_J + 1)
.define MEM_CALL_STACK_END, (MEM_CALL_STACK_PTR + MEM_CALL_STACK_LEN)
.define_eval MAIN_JUMPER, (CORE_MEM_PRG_END - 3 - 1)
.define MEM_ALLOC_PTR, MEM_CALL_STACK_END
.define_eval MEM_LOOP_DEPTH, CORE_MEM_MEM
.define_eval MEM_LOOP_I, (MEM_LOOP_DEPTH + 1)
.define_eval MEM_LOOP_J, (MEM_LOOP_I + 1)
.define_eval MEM_CALL_STACK_LEN, 16
.define_eval MEM_CALL_STACK_PTR, (MEM_LOOP_J + 1)
.define_eval MEM_CALL_STACK_END, (MEM_CALL_STACK_PTR + MEM_CALL_STACK_LEN)
.define_eval MEM_ALLOC_PTR, (MEM_CALL_STACK_END + 1)
.macro stack_transfer_alu
.std_ld
tlr CORE_REG_B
.std_ld
tlr CORE_REG_A
.endmacro
.std_rset CORE_REG_A, MEM_CALL_STACK_PTR
.std_set MEM_CALL_STACK_PTR
.std_rset CORE_REG_A, (MEM_ALLOC_PTR + 1)
.std_rset CORE_REG_A, MEM_ALLOC_PTR
.std_set MEM_ALLOC_PTR
.jump_main
.std_jump MAIN_JUMPER
call_stack_jump:
.std_get MEM_CALL_STACK_PTR
@ -52,13 +70,6 @@ return_call_stack_jump:
get
tlr CORE_REG_PC
.macro stack_transfer_alu
.std_ld
tlr CORE_REG_B
.std_ld
tlr CORE_REG_A
.endmacro
.macro call_stack_jump, call_stack_jump_arg_0_label, call_stack_jump_arg_1_offset
.std_rset CORE_REG_C, call_stack_jump_arg_0_label
.std_rset CORE_REG_D, (call_stack_jump_arg_1_offset + 7)
@ -66,6 +77,48 @@ return_call_stack_jump:
tlr CORE_REG_PC
.endmacro
.macro call_stack_jump_cond_if, call_stack_jump_cond_if_arg_0_if_label, call_stack_jump_cond_if_arg_1_offset
.std_ld
tlr CORE_REG_A
.std_rset CORE_REG_C, call_stack_jump_cond_if_arg_0_if_label
.std_rset CORE_REG_D, (call_stack_jump_cond_if_arg_1_offset + 7)
ts call_stack_jump
tlrc CORE_REG_PC
.endmacro
.define_eval prev, OFFSET
.std_ld
tlr CORE_REG_A
.std_rset CORE_REG_C, CORE_U16_MAX
.std_rset CORE_REG_D, CORE_U16_MAX
ts call_stack_jump
tlrc CORE_REG_PC
.std_rset CORE_REG_C, CORE_U16_MAX
.std_rset CORE_REG_D, CORE_U16_MAX
ts call_stack_jump
tlr CORE_REG_PC
.define_eval CALL_STACK_JUMP_COND_IF_ELSE_SIZE, (OFFSET - prev)
.delete prev
.macro call_stack_jump_cond_if_else, call_stack_jump_cond_if_else_arg_0_if_label, call_stack_jump_cond_if_else_arg_1_else_label, call_stack_jump_cond_if_else_arg_2_offset
.define_eval prev, OFFSET
.std_ld
tlr CORE_REG_A
.std_rset CORE_REG_C, call_stack_jump_cond_if_else_arg_0_if_label
.std_rset CORE_REG_D, (call_stack_jump_cond_if_else_arg_2_offset)
ts call_stack_jump
tlrc CORE_REG_PC
.std_rset CORE_REG_C, call_stack_jump_cond_if_else_arg_1_else_label
.std_rset CORE_REG_D, (call_stack_jump_cond_if_else_arg_2_offset + 11)
ts call_stack_jump
tlr CORE_REG_PC
.delete prev
.endmacro
.macro return_call_stack_jump
.std_jump return_call_stack_jump
.endmacro
@ -89,13 +142,15 @@ return_call_stack_jump:
; condition: {{ c.id }}
conditions_if_{{ c.id }}:
{%- for ins in c.data.0 %}
{{ ins.to_code() }}
{{ ins }}
{%- endfor %}
.return_call_stack_jump
conditions_else_{{ c.id }}:
{%- for ins in c.data.0 %}
{{ ins.to_code() }}
{{ ins }}
{%- endfor %}
.return_call_stack_jump
{% endfor %}
; words
@ -104,15 +159,19 @@ return_call_stack_jump:
{% let name = snailquote::escape(w.data.0) -%}
; name : {% call escape_name(name) %}
{%- for node in w.data.1 %}
{{ node.to_code() }}
{{ node }}
{%- endfor %}
.return_call_stack_jump
.return_call_stack_jump
{% endfor %}
; main
.def_main
ts NULL ; reset tmp
{% for node in main %}
{{ node.to_code() }}
main:
.org MAIN_JUMPER
ts main
tlr CORE_REG_PC
.org main
{%- for node in main %}
{{ node }}
{%- endfor %}
.std_stop