Skip to content

Commit

Permalink
Replacing newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
3top1a committed Jan 15, 2025
1 parent 6f89f34 commit b4d8dd3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
6 changes: 6 additions & 0 deletions examples/basic.🍺
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ y = 5
// This will generate a single - because it's faster
z = 255
array = [1 2 3 4 5]
c = 'c'
basm(".")
newline = '\n'
basm(".")
x = "\nHello, World!"
basm("<<<<<<<<<<<<.>.>.>.>.>.>.>.>.>.>.>.>.")
9 changes: 9 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,12 @@ pub enum Iterator {
},
Path(Box<Expression>),
}

impl Expression {
pub fn as_number(&self) -> Option<u8> {
match self {
Expression::Number(n) => Some(*n),
_ => None,
}
}
}
24 changes: 23 additions & 1 deletion src/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::str::from_utf8;
use crate::ast::Expression;

pub struct Codegen {
Expand Down Expand Up @@ -149,6 +150,25 @@ impl Codegen {
Expression::Number(x) => {
self.set_new_value(*x);
}
Expression::Path(x) => {
self.move_to(*self.variables.get(x).unwrap());
}
Expression::Call { name, args } => {
match name.as_str() {
"basm" => {
assert_eq!(args.len(), 1);
match args.get(0).unwrap() {
Expression::Array(x) => {
for value in x.iter() {
self.code += from_utf8(&[value.as_number().unwrap()]).unwrap()
}
}
_ => { todo!() }
}
}
_ => { todo!("Implement other functions"); }
}
}
Expression::Assignment { name, value } => {
if self.variables.get(name).is_none() {
match *value.clone() {
Expand Down Expand Up @@ -176,7 +196,9 @@ impl Codegen {
self.add_value(3);
}
}
_ => {}
_ => {
todo!("Implement other expressions");
}
}
}
}
15 changes: 14 additions & 1 deletion src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn tokenize_indexed(input: &str) -> Vec<IndexedToken> {
}
})
.collect();
output
postprocess(output)
}

#[allow(dead_code)]
Expand All @@ -42,3 +42,16 @@ pub fn tokenize(input: &str) -> Vec<Token> {
.map(|x| x.token)
.collect()
}

fn postprocess(mut input: Vec<IndexedToken>) -> Vec<IndexedToken> {
for token in input.iter_mut() {
match token.token {
Token::String(ref mut x) => {
*x = x.replace("\\n", "\n");
}
_ => {}
}
}

input
}
3 changes: 2 additions & 1 deletion src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ pub enum Token {
#[regex(r"[0-9]+", |lex| lex.slice().parse().ok())]
Integer(u8),

#[regex(r"'(.|\\n|\\d)'", |lex| lex.slice().chars().nth(1).map(|c| c as u8))]
#[regex(r"'(.)'", |lex| lex.slice().chars().nth(1).map(|c| c as u8))]
#[regex(r"'\\n'", |_| Some(10))]
Char(u8),

#[regex(r#""[^"]*""#, |lex| {
Expand Down

0 comments on commit b4d8dd3

Please sign in to comment.