Skip to content

Commit

Permalink
Const program
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Nov 10, 2023
1 parent ec6667e commit baaa001
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
dm42
45 changes: 45 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
[dependencies]
anyhow = "1.0.75"
clap = { version = "4.4.7", features = ["derive"] }
regex = "1.10.2"
27 changes: 27 additions & 0 deletions bin/const.dm42
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Allows using various constants
// Flag 3 - Exit to custom menu

export def const {
CLMENU
"c", KEY 1 GTO light_speed
"h", KEY 2 GTO planck_constant
"e", KEY 3 GTO elementary_charge
"N↓A", KEY 4 GTO avogadro_constant
// KEY 8 GTO page_2
if { FS? 03 } {
KEY 9 GTO custom_menu
}
MENU
STOP
}

def custom_menu {
SF 27
}

// == CONSTANTS ==

def light_speed { 299792458 }
def planck_constant { 6.62607015e-34 }
def elementary_charge { 1.602176634e-19 }
def avogadro_constant { 6.02214076e23 }
File renamed without changes.
59 changes: 30 additions & 29 deletions out.free42
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
LBL "sort"
MAT?
GTO 00
GTO 01
LBL "const"
CLMENU
"c"
KEY 1 GTO 01
"h"
KEY 2 GTO 02
"e"
KEY 3 GTO 03
"N↓A"
KEY 4 GTO 04
FS? 03
GTO 06
LBL 05
MENU
STOP
RTN
LBL 00
STO "A"
INDEX "A"
1
1
STOIJ
DROPN 2
GTO 03
SF 27
RTN
LBL 01
299792458
RTN
LBL 02
DROPN 2
[MIN]
DROP
RCLIJ
DROP
R<>R
DROPN 2
I+
6.62607015e-34
RTN
LBL 03
FC? 76
GTO 02
DROPN 2
RCL "A"
1.602176634e-19
RTN
LBL 01
DROPN 2
"X is not a matrix"
AVIEW
STOP
GTO 00
LBL 04
6.02214076e23
RTN
LBL 06
KEY 9 GTO 00
GTO 05
RTN
33 changes: 28 additions & 5 deletions src/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::borrow::Cow;

use regex::Regex;

use crate::{
ident::FreeIdent,
misc::OrderedMap,
Expand Down Expand Up @@ -40,7 +42,9 @@ impl CodeGen {
}

fn get_function(&mut self, name: &str) -> &mut Function {
self.functions.get_mut(name).expect("Function not found")
self.functions
.get_mut(name)
.expect(format!("Function `{}` not found", name).as_str())
}

fn new_ident(&mut self) -> String {
Expand Down Expand Up @@ -102,6 +106,7 @@ fn _generate(codegen: &mut CodeGen, tokens: Vec<Token>, function: String) {
if body.is_empty() && else_body.is_empty() {
continue;
}
let is_raw = condition.is_raw();
gen_condition(codegen, condition, function.clone());
let end_label = codegen.new_ident();

Expand All @@ -111,7 +116,9 @@ fn _generate(codegen: &mut CodeGen, tokens: Vec<Token>, function: String) {
push_ins(codegen, format!("GTO {true_label}"));

let mut fun = Function::new_private(true_label.clone());
fun.ins("DROPN 2".to_owned());
if !is_raw {
fun.ins("DROPN 2".to_owned());
}
codegen.functions.insert(true_label.clone(), fun);
_generate(codegen, body, true_label.clone());
codegen
Expand All @@ -128,14 +135,16 @@ fn _generate(codegen: &mut CodeGen, tokens: Vec<Token>, function: String) {
push_ins(codegen, format!("GTO {false_label}"));

let mut fun = Function::new_private(false_label.clone());
fun.ins("DROPN 2".to_owned());
if !is_raw {
fun.ins("DROPN 2".to_owned());
}
codegen.functions.insert(false_label.clone(), fun);
_generate(codegen, else_body, false_label.clone());
codegen
.get_function(&false_label)
.body
.push(format!("GTO {end_label}"));
} else {
} else if !is_raw {
push_ins(codegen, "DROPN 2".to_owned());
}

Expand Down Expand Up @@ -164,11 +173,25 @@ fn _generate(codegen: &mut CodeGen, tokens: Vec<Token>, function: String) {
push_ins(codegen, format!("GTO {loop_start}"));
push_ins(codegen, "DROPN 2".to_owned());
}
Token::Instruction(ins) => codegen.get_function(&function).body.push(ins),
Token::Instruction(ins) => {
let ins = transform_instruction(codegen, ins);
push_ins(codegen, ins);
}
}
}
}

fn transform_instruction(codegen: &mut CodeGen, mut ins: String) -> String {
// Todo, clean this up
for (name, func) in codegen.functions.iter() {
let regex = Regex::new(&format!("\\b{name}\\b")).unwrap();
ins = regex
.replace(&ins, format!("{}", func.ident()))
.into_owned();
}
ins
}

fn gen_condition(codegen: &mut CodeGen, condition: Condition, function: String) {
match condition {
Condition::Comparison { a, b, comparison } => {
Expand Down
6 changes: 6 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ pub enum Comparison {
Lte,
}

impl Condition {
pub fn is_raw(&self) -> bool {
matches!(self, Condition::Raw { .. })
}
}

impl Comparison {
pub fn instruction(&self) -> &str {
match self {
Expand Down
2 changes: 0 additions & 2 deletions src/tokenize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use anyhow::{bail, Context, Result};

use crate::token::{Comparison, Condition, Token};

const COMMENT_CHARS: &[char] = &['#', ';', '@'];

pub struct Tokenizer {
chars: Vec<char>,
idx: usize,
Expand Down

0 comments on commit baaa001

Please sign in to comment.