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, Ptr,
Operator, Operator,
SecondOperator, SecondOperator,
Semicolon,
Round, Round,
Curly, Curly,
Square, Square,
@@ -35,13 +36,13 @@ pub struct Token {
impl fmt::Debug for Token { impl fmt::Debug for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 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 { impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 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> token_regex: Lazy<Regex>
} }
const SYNTAX: [Node; 12] = [ const SYNTAX: [Node; 13] = [
Node { Node {
token_type: TokenType::Newline, token_type: TokenType::Newline,
token_regex: Lazy::new(|| Regex::new(r"^\n+").unwrap()), token_regex: Lazy::new(|| Regex::new(r"^\n+").unwrap()),
@@ -77,27 +78,31 @@ const SYNTAX: [Node; 12] = [
}, },
Node { Node {
token_type: TokenType::Operator, token_type: TokenType::Operator,
token_regex: Lazy::new(|| Regex::new(r"^[\-|\+|\*]").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^[\-|\+|\*|\=]").unwrap())
}, },
Node { Node {
token_type: TokenType::Round, token_type: TokenType::Round,
token_regex: Lazy::new(|| Regex::new(r"\((?:[^()]|(?R))*\)").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^\((?:[^()]|(?R))*\)").unwrap())
}, },
Node { Node {
token_type: TokenType::Curly, token_type: TokenType::Curly,
token_regex: Lazy::new(|| Regex::new(r"\{(?:[^{}]|(?R))*}").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^\{(?:[^{}]|(?R))*}").unwrap())
}, },
Node { Node {
token_type: TokenType::Square, token_type: TokenType::Square,
token_regex: Lazy::new(|| Regex::new(r"\[(?:[^\[\]]|(?R))*]").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^\[(?:[^\[\]]|(?R))*]").unwrap())
}, },
Node { Node {
token_type: TokenType::Angle, 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 { Node {
token_type: TokenType::SecondOperator, token_type: TokenType::SecondOperator,
token_regex: Lazy::new(|| Regex::new(r"^,|;").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^,").unwrap())
}, },
]; ];
+7 -10
View File
@@ -1,14 +1,11 @@
mod lexer; mod transpiler;
use lexer::lex;
mod parser; mod parser;
use parser::Parser; mod lexer;
use std::fs;
fn main() { fn main() {
let input = "int<x> mut x"; let contents = fs::read_to_string("code.ws").expect("cannot read for some reason");
println!("\n\n\n\n"); let result = transpiler::transpile(contents, 0);
let tokens = lex(input, false); println!("{result}")
let mut ast = Parser::new(tokens.clone());
while ast.tokens.len() > ast.index as usize {
println!("{}\n\n", ast.next());
}
} }
+7 -2
View File
@@ -4,6 +4,7 @@ use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum AstType { pub enum AstType {
FunctionDeceleration, FunctionDeceleration,
VoidFunctionDeceleration,
VariableDeceleration, VariableDeceleration,
MutVariableDeceleration, MutVariableDeceleration,
Other Other
@@ -59,12 +60,16 @@ impl Parser {
let token = &self.tokens[index]; let token = &self.tokens[index];
match token.token_type { match token.token_type {
TokenType::Identifier => { 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].clone());
ast_res.tokens.push(self.tokens[index+1].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+2].clone());
ast_res.tokens.push(self.tokens[index+3].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; self.index += 3;
} else { } else {
loop { loop {