codegen alpha

This commit is contained in:
Leo
2024-04-10 18:32:47 +02:00
parent de2889c5d8
commit e2f87dc818
3 changed files with 29 additions and 22 deletions
+14 -9
View File
@@ -18,6 +18,7 @@ pub enum TokenType {
Ptr,
Operator,
SecondOperator,
Semicolon,
Round,
Curly,
Square,
@@ -35,13 +36,13 @@ pub struct Token {
impl fmt::Debug for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(TokenType: {:?}, TokenValue: {}, Line: {}, Column {})", self.token_type, self.value, self.line, self.column)
write!(f, "(TokenType: {:?}, TokenValue: {}, Line: {}, Column {})", self.token_type, self.value.replace("\n", "\\n"), self.line, self.column)
}
}
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(\x1b[33mTokenType:\x1b[0m \x1b[35m{:?}\x1b[0m, \x1b[33mTokenValue:\x1b[0m \x1b[32m\"{}\"\x1b[0m, \x1b[33mLine:\x1b[0m \x1b[36m{}\x1b[0m, \x1b[33mColumn:\x1b[0m \x1b[36m{}\x1b[0m)", self.token_type, self.value, self.line, self.column)
write!(f, "(\x1b[33mTokenType:\x1b[0m \x1b[35m{:?}\x1b[0m, \x1b[33mTokenValue:\x1b[0m \x1b[32m\"{}\"\x1b[0m, \x1b[33mLine:\x1b[0m \x1b[36m{}\x1b[0m, \x1b[33mColumn:\x1b[0m \x1b[36m{}\x1b[0m)", self.token_type, self.value.replace("\n", "\\n"), self.line, self.column)
}
}
@@ -50,7 +51,7 @@ pub struct Node {
token_regex: Lazy<Regex>
}
const SYNTAX: [Node; 12] = [
const SYNTAX: [Node; 13] = [
Node {
token_type: TokenType::Newline,
token_regex: Lazy::new(|| Regex::new(r"^\n+").unwrap()),
@@ -77,27 +78,31 @@ const SYNTAX: [Node; 12] = [
},
Node {
token_type: TokenType::Operator,
token_regex: Lazy::new(|| Regex::new(r"^[\-|\+|\*]").unwrap())
token_regex: Lazy::new(|| Regex::new(r"^[\-|\+|\*|\=]").unwrap())
},
Node {
token_type: TokenType::Round,
token_regex: Lazy::new(|| Regex::new(r"\((?:[^()]|(?R))*\)").unwrap())
token_regex: Lazy::new(|| Regex::new(r"^\((?:[^()]|(?R))*\)").unwrap())
},
Node {
token_type: TokenType::Curly,
token_regex: Lazy::new(|| Regex::new(r"\{(?:[^{}]|(?R))*}").unwrap())
token_regex: Lazy::new(|| Regex::new(r"^\{(?:[^{}]|(?R))*}").unwrap())
},
Node {
token_type: TokenType::Square,
token_regex: Lazy::new(|| Regex::new(r"\[(?:[^\[\]]|(?R))*]").unwrap())
token_regex: Lazy::new(|| Regex::new(r"^\[(?:[^\[\]]|(?R))*]").unwrap())
},
Node {
token_type: TokenType::Angle,
token_regex: Lazy::new(|| Regex::new(r"<(?:[^<>]|(?R))*>").unwrap())
token_regex: Lazy::new(|| Regex::new(r"^<(?:[^<>]|(?R))*>").unwrap())
},
Node {
token_type: TokenType::Semicolon,
token_regex: Lazy::new(|| Regex::new(r"^\;").unwrap())
},
Node {
token_type: TokenType::SecondOperator,
token_regex: Lazy::new(|| Regex::new(r"^,|;").unwrap())
token_regex: Lazy::new(|| Regex::new(r"^,").unwrap())
},
];
+8 -11
View File
@@ -1,14 +1,11 @@
mod lexer;
use lexer::lex;
mod transpiler;
mod parser;
use parser::Parser;
mod lexer;
use std::fs;
fn main() {
let input = "int<x> mut x";
println!("\n\n\n\n");
let tokens = lex(input, false);
let mut ast = Parser::new(tokens.clone());
while ast.tokens.len() > ast.index as usize {
println!("{}\n\n", ast.next());
}
}
let contents = fs::read_to_string("code.ws").expect("cannot read for some reason");
let result = transpiler::transpile(contents, 0);
println!("{result}")
}
+7 -2
View File
@@ -4,6 +4,7 @@ use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AstType {
FunctionDeceleration,
VoidFunctionDeceleration,
VariableDeceleration,
MutVariableDeceleration,
Other
@@ -59,12 +60,16 @@ impl Parser {
let token = &self.tokens[index];
match token.token_type {
TokenType::Identifier => {
if self.tokens.len()-index > 2 && self.tokens[index+1].token_type==TokenType::Identifier && self.tokens[index+2].token_type==TokenType::Round && self.tokens[index+3].token_type==TokenType::Curly {
if self.tokens.len()-index > 3 && self.tokens[index+1].token_type==TokenType::Identifier && self.tokens[index+2].token_type==TokenType::Round && self.tokens[index+3].token_type==TokenType::Curly {
ast_res.tokens.push(self.tokens[index].clone());
ast_res.tokens.push(self.tokens[index+1].clone());
ast_res.tokens.push(self.tokens[index+2].clone());
ast_res.tokens.push(self.tokens[index+3].clone());
ast_res.ast_type = AstType::FunctionDeceleration;
if token.value == "void" {
ast_res.ast_type = AstType::VoidFunctionDeceleration;
} else {
ast_res.ast_type = AstType::FunctionDeceleration;
}
self.index += 3;
} else {
loop {