rewrive-rev01

This commit is contained in:
2024-06-25 21:13:46 +02:00
parent 15e0ff362c
commit 910b638458
6 changed files with 319 additions and 112 deletions
+3
View File
@@ -7,5 +7,8 @@
<h3 align="center">Wyst is a programing language that has similar syntax to C and transpiles to Rust</h3> <h3 align="center">Wyst is a programing language that has similar syntax to C and transpiles to Rust</h3>
# Re Write
We are re-writing the parser and transpiler due to the messy code that is very hard to expand
# Alpha # Alpha
We are on pure alpha. if you would like to contribute please open a new issue. We are on pure alpha. if you would like to contribute please open a new issue.
+2 -1
View File
@@ -85,7 +85,8 @@ const SYNTAX: [Node; 17] = [
Node { Node {
token_type: TokenType::Keyword, token_type: TokenType::Keyword,
token_regex: Lazy::new(|| { token_regex: Lazy::new(|| {
Regex::new(r"^(pub|mut|try|catch|return|fn|let|use|cb|struct|impl|in|as)\b").unwrap() Regex::new(r"^(pub|mut|try|catch|return|fn|let|use|cb|enum|struct|impl|in|as)\b")
.unwrap()
}), }),
}, },
Node { Node {
+246 -111
View File
@@ -1,120 +1,255 @@
mod compile; #[derive(Debug, PartialEq, Clone)]
mod dllmgr; enum Token {
mod file_writer; Plus,
mod lexer; Minus,
mod lsp; Star,
mod lspcom; Slash,
mod parser; Equals,
mod transpiler; Semicolon,
mod variable; LParen,
use clap::Parser; RParen,
use std::{fs, path::Path}; Identifier(String),
use transpiler::Transpiler; Number(i64),
use variable::{Variable, VariableType, Variables}; EOF,
use crate::lsp::run_lsp_server;
// Arguments for Wyst (short and long version)
#[derive(Parser)]
struct Args {
//#[clap(short, long)]
//rust: Option<String>,
#[clap(short, long)]
compile: Option<String>,
#[clap(short, long)]
dll: Option<String>,
#[clap(long)]
stdio: bool,
} }
fn main() { fn tokenize(input: &str) -> Vec<Token> {
let args = Args::parse(); let mut tokens = Vec::new();
match args.stdio { let chars: Vec<char> = input.chars().collect();
true => { let mut i = 0;
run_lsp_server();
}
false => {
match args.compile {
Some(ref exe_name) => {
let file_content = fs::read_to_string("main.wt").expect("Error reading file");
if Path::new("build").exists() {
fs::remove_dir_all("build").expect("err rm build");
}
fs::create_dir("build").expect("error making build");
let mut trsp = Transpiler::default();
let mut vars = Variables::new();
let mut transpiled_code = trsp.transpile(file_content, 0, &mut vars);
transpiled_code += "\nfn main() {";
transpiled_code += "std::process::exit(";
transpiled_code += vars.get_var("main".to_string(), &mut trsp).as_str();
transpiled_code += "())}";
for problem in &trsp.problems {
println!("{:?}: {}", problem.problem_type, problem.problem_msg)
}
if trsp.problems.len() > 0 {
return;
}
trsp.writer.write();
compile::write_to_rust_file(&transpiled_code, "build/main.rs") while i < chars.len() {
.expect("Error writing to temporary Rust file"); match chars[i] {
std::env::set_current_dir("build").expect("setDir err: "); ' ' | '\t' | '\n' | '\r' => i += 1,
compile::compile_to_executable(exe_name) '+' => {
.expect("Error compiling to executable"); tokens.push(Token::Plus);
std::env::set_current_dir("..").expect("setDir0 err: "); i += 1;
fs::rename(Path::new("build").join(exe_name).as_path(), exe_name)
.expect("RenameErrBuld: ");
fs::remove_dir_all("build").expect("err rm build");
}
None => {}
} }
match args.dll { '-' => {
Some(ref dll_path) => { tokens.push(Token::Minus);
let file_content = fs::read_to_string("lib.wt").expect("Error reading file"); i += 1;
if Path::new("build").exists() { }
fs::remove_dir_all("build").expect("err rm build"); '*' => {
} tokens.push(Token::Star);
fs::create_dir("build").expect("error making build"); i += 1;
let mut trsp = Transpiler::default(); }
let mut vars = Variables::new(); '/' => {
let transpiled_code = trsp.transpile(file_content, 0, &mut vars); tokens.push(Token::Slash);
for problem in trsp.problems { i += 1;
println!("{}", problem.problem_msg) }
} '=' => {
trsp.writer.write(); tokens.push(Token::Equals);
let mut dll_main = String::from( i += 1;
"mod wslib;use wslib::*;\nfn call_fn(fn_name: &str, params: Vec<Param>)->i32{match fn_name {", }
); ';' => {
for (name, var) in vars.vars.clone() { tokens.push(Token::Semicolon);
if var.vtype != VariableType::Func { i += 1;
continue; }
} '(' => {
let mut dparams = String::new(); tokens.push(Token::LParen);
println!("{:?}", var.params.vars); i += 1;
for i in 0..var.params.vars.len() { }
dparams += format!("params.get({}).expect(\"Err_prms\"),", i).as_str(); ')' => {
} tokens.push(Token::RParen);
dll_main += format!( i += 1;
"\"{}\" => {}return {}({dparams});{}", }
name, "{", var.rname, "}" '0'..='9' => {
) let mut num = 0;
.as_str(); while i < chars.len() && chars[i].is_digit(10) {
} num = num * 10 + chars[i].to_digit(10).unwrap() as i64;
dll_main += "}}\nfn main(){}"; i += 1;
//dll_main = "fn main(){}".to_string();
compile::write_to_rust_file(&transpiled_code, "build/wslib.rs")
.expect("Error writing to temporary Rust file");
compile::write_to_rust_file(&dll_main, "build/main.rs")
.expect("Error writing to main dll");
std::env::set_current_dir("build").expect("setDir err: ");
compile::compile_to_executable("run").expect("Error compiling to executable");
dllmgr::write_dll(vars, "run".to_string(), dll_path.to_string());
// fs::remove_dir_all("build").expect("err rm build");
} }
None => {} tokens.push(Token::Number(num));
}
'a'..='z' | 'A'..='Z' => {
let mut id = String::new();
while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '_') {
id.push(chars[i]);
i += 1;
}
match id.as_str() {
_ => tokens.push(Token::Identifier(id)),
}
}
_ => panic!("Unexpected character: {}", chars[i]),
}
}
tokens.push(Token::EOF);
tokens
}
#[derive(Debug)]
enum ASTNode {
Number(i64),
Identifier(String),
BinaryOp(Box<ASTNode>, Token, Box<ASTNode>),
Assignment(String, Box<ASTNode>),
Print(Box<ASTNode>),
Let(String, Box<ASTNode>),
Sequence(Vec<ASTNode>),
}
struct Parser {
tokens: Vec<Token>,
current_token: Token,
pos: usize,
}
impl Parser {
fn new(tokens: Vec<Token>) -> Self {
Self {
tokens: tokens.clone(),
current_token: tokens[0].clone(),
pos: 0,
}
}
fn eat(&mut self, token: Token) {
if self.current_token == token {
self.pos += 1;
self.current_token = self.tokens[self.pos].clone();
} else {
panic!("Unexpected token: {:?}", self.current_token);
}
}
fn factor(&mut self) -> ASTNode {
match self.current_token.clone() {
Token::Number(value) => {
self.eat(Token::Number(value));
ASTNode::Number(value)
}
Token::Identifier(name) => {
self.eat(Token::Identifier(name.clone()));
ASTNode::Identifier(name)
}
Token::LParen => {
self.eat(Token::LParen);
let expr = self.expression();
self.eat(Token::RParen);
expr
}
_ => panic!("Unexpected token: {:?}", self.current_token),
}
}
fn term(&mut self) -> ASTNode {
let mut node = self.factor();
while self.current_token == Token::Star || self.current_token == Token::Slash {
let token = self.current_token.clone();
self.eat(token.clone());
node = ASTNode::BinaryOp(Box::new(node), token, Box::new(self.factor()));
}
node
}
fn expression(&mut self) -> ASTNode {
let mut node = self.term();
while self.current_token == Token::Plus || self.current_token == Token::Minus {
let token = self.current_token.clone();
self.eat(token.clone());
node = ASTNode::BinaryOp(Box::new(node), token, Box::new(self.term()));
}
node
}
fn statement(&mut self) -> ASTNode {
match self.current_token.clone() {
Token::Identifier(name) => {
self.eat(Token::Identifier(name.clone()));
self.eat(Token::Equals);
let expr = self.expression();
self.eat(Token::Semicolon);
ASTNode::Assignment(name, Box::new(expr))
}
_ => panic!("Unexpected token: {:?}", self.current_token),
}
}
fn parse(&mut self) -> ASTNode {
let mut statements = Vec::new();
while self.current_token != Token::EOF {
statements.push(self.statement());
}
ASTNode::Sequence(statements)
}
}
use std::collections::HashMap;
struct Interpreter {
symbol_table: HashMap<String, i64>,
}
impl Interpreter {
fn new() -> Self {
Self {
symbol_table: HashMap::new(),
}
}
fn evaluate(&mut self, node: &ASTNode) -> i64 {
match node {
ASTNode::Number(value) => *value,
ASTNode::Identifier(name) => *self.symbol_table.get(name).expect("Undefined variable"),
ASTNode::BinaryOp(left, token, right) => {
let left_val = self.evaluate(left);
let right_val = self.evaluate(right);
match token {
Token::Plus => left_val + right_val,
Token::Minus => left_val - right_val,
Token::Star => left_val * right_val,
Token::Slash => left_val / right_val,
_ => panic!("Unexpected binary operator"),
}
}
ASTNode::Assignment(name, expr) => {
let value = self.evaluate(expr);
self.symbol_table.insert(name.clone(), value);
value
}
ASTNode::Let(name, expr) => {
let value = self.evaluate(expr);
self.symbol_table.insert(name.clone(), value);
value
}
ASTNode::Print(expr) => {
let value = self.evaluate(expr);
println!("{}", value);
value
}
ASTNode::Sequence(statements) => {
for statement in statements {
self.evaluate(statement);
}
0
} }
} }
} }
} }
fn main() {
let input = "
let x = 5;
let y = x + 3;
print y;
x = y * 2;
print x;
";
let tokens = tokenize(input);
println!("Tokens: {:?}", tokens);
let mut parser = Parser::new(tokens);
let ast = parser.parse();
println!("AST: {:?}", ast);
let mut interpreter = Interpreter::new();
interpreter.evaluate(&ast);
}
+22
View File
@@ -26,6 +26,7 @@ pub enum AstType {
Json, Json,
Impl, Impl,
StaticExecution, StaticExecution,
EnumDeceleration,
Other, Other,
} }
@@ -119,6 +120,27 @@ impl Parser {
ast_res.tokens.push(self.tokens[index + 1].clone()); ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.ast_type = AstType::Ref; ast_res.ast_type = AstType::Ref;
self.index += 1; self.index += 1;
} else if self.tokens.len() - index > 2
&& self.tokens[index].value == "enum"
&& self.tokens[index + 1].token_type == TokenType::Identifier
&& self.tokens[index + 2].token_type == TokenType::Curly
{
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.ast_type = AstType::EnumDeceleration;
self.index += 2;
let mut desc = String::new();
if index > 0 && self.tokens[index - 1].token_type == TokenType::Comment {
desc = self.tokens[index - 1].value.clone()
}
self.variables.new_enum(
self.tokens[index + 1].clone().value,
LexerState {
line: self.tokens[index + 1].clone().line,
column: self.tokens[index + 1].clone().column,
},
desc,
);
} else if self.tokens.len() - index > 2 } else if self.tokens.len() - index > 2
&& self.tokens[index].value == "struct" && self.tokens[index].value == "struct"
&& self.tokens[index + 1].token_type == TokenType::Identifier && self.tokens[index + 1].token_type == TokenType::Identifier
+31
View File
@@ -149,6 +149,37 @@ impl Transpiler {
) )
) )
.as_str(); .as_str();
} else if ast.ast_type == AstType::StructDeceleration {
if self.auto_pub {
result += "pub ";
}
let mut vars: Variables = variables.clone();
let round = self.transpile_round(ast.tokens[1].value.clone(), &mut vars);
result += format!(
"enum {} {} {}",
ast.tokens[0].value,
"{\n",
round.trim_end()
)
.replace(
"\n",
("\n".to_string() + " ".repeat(((indent + 1) as usize) * 2).as_str())
.as_str(),
)
.as_str();
result += "\n}\n";
let vvars = variables.clone();
if let Some(v) = variables.get_mut(ast.tokens[0].value.clone()) {
for (name, var) in vars.iter_mut() {
if !(vvars.vars.contains_key(name)) {
v.params.new_var(
name.to_string(),
LexerState { line: 0, column: 0 },
var.desc.clone(),
);
}
}
}
} else if ast.ast_type == AstType::StructDeceleration { } else if ast.ast_type == AstType::StructDeceleration {
if self.auto_pub { if self.auto_pub {
result += "pub "; result += "pub ";
+15
View File
@@ -16,6 +16,7 @@ pub enum VariableType {
Keyword, Keyword,
Struct, Struct,
Namespace, Namespace,
Enum,
} }
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
@@ -93,6 +94,20 @@ impl Variables {
}, },
); );
} }
pub fn new_enum(&mut self, name: String, state: LexerState, desc: String) {
self.vars.insert(
name,
Variable {
vtype: VariableType::Enum,
desc,
state,
params: Variables {
vars: HashMap::new(),
},
rname: generate_varname(),
},
);
}
// pub fn new_keyword(&mut self, name: String, state: LexerState, desc: String) { // pub fn new_keyword(&mut self, name: String, state: LexerState, desc: String) {
// self.vars.insert( // self.vars.insert(