better errors

This commit is contained in:
2024-04-14 00:11:39 +02:00
parent db3f1ec61c
commit b24b34cb44
3 changed files with 78 additions and 67 deletions
+4 -4
View File
@@ -121,7 +121,7 @@ fn get_second_char(value: &str) -> String {
sv2.next().unwrap().to_string() sv2.next().unwrap().to_string()
} }
pub fn lex(mut code: &str, use_whitespace: bool) -> Vec<Token> { pub fn lex(mut code: &str, use_whitespace: bool) -> Result<Vec<Token>, (LexerState, Vec<Token>)> {
let mut state = LexerState { line: 1, column: 1 }; let mut state = LexerState { line: 1, column: 1 };
let mut tokens: Vec<Token> = Vec::new(); let mut tokens: Vec<Token> = Vec::new();
let mut bracket_vec: Vec<char> = Vec::new(); let mut bracket_vec: Vec<char> = Vec::new();
@@ -292,9 +292,9 @@ pub fn lex(mut code: &str, use_whitespace: bool) -> Vec<Token> {
} }
} }
if !is_match { if !is_match {
println!("Error: syntax ->{code}"); return Err((state, tokens));
break; // break;
} }
} }
tokens Ok(tokens)
} }
+2 -1
View File
@@ -5,7 +5,8 @@ use std::fs;
fn main() { fn main() {
let contents = fs::read_to_string("code.ws").expect("cannot read for some reason"); let code_file = "code.ws";
let contents = fs::read_to_string(code_file).expect("cannot read for some reason");
let result = transpiler::transpile(contents, 0); let result = transpiler::transpile(contents, 0);
println!("{result}") println!("{result}")
} }
+74 -64
View File
@@ -3,73 +3,83 @@ use crate::parser::{Parser, AstType};
pub fn transpile(input: String, indent: u32) -> String { pub fn transpile(input: String, indent: u32) -> String {
let mut result = String::new(); let mut result = String::new();
let tokens = lex(input.as_str(), false); let lexer_out = lex(input.as_str(), false);
println!("\n\n\n\n"); match lexer_out {
let mut full_ast = Parser::new(tokens.clone()); Ok(tokens) => {
while full_ast.tokens.len() > full_ast.index as usize { println!("\n\n\n\n");
let ast = full_ast.next(); let mut full_ast = Parser::new(tokens.clone());
if ast.ast_type == AstType::FunctionDeceleration { while full_ast.tokens.len() > full_ast.index as usize {
result += format!("fn {}{} -> {} {}", ast.tokens[1].value, ast.tokens[2].value, ast.tokens[0].value, transpile(ast.tokens[3].value.clone(), indent+1)).as_str(); let ast = full_ast.next();
} else if ast.ast_type == AstType::VoidFunctionDeceleration { println!("{ast}");
result += format!("fn {}{} {}", ast.tokens[1].value, ast.tokens[2].value, transpile(ast.tokens[3].value.clone(), indent+1)).as_str(); if ast.ast_type == AstType::FunctionDeceleration {
} else if ast.ast_type == AstType::VariableDeceleration { result += format!("fn {}{} -> {} {}", ast.tokens[1].value, ast.tokens[2].value, ast.tokens[0].value, transpile(ast.tokens[3].value.clone(), indent+1)).as_str();
let _o = String::new(); } else if ast.ast_type == AstType::VoidFunctionDeceleration {
let mut oy = ast.tokens.clone(); result += format!("fn {}{} {}", ast.tokens[1].value, ast.tokens[2].value, transpile(ast.tokens[3].value.clone(), indent+1)).as_str();
oy.remove(0); } else if ast.ast_type == AstType::VariableDeceleration {
if oy.len() > 1 { let _o = String::new();
result += "("; let mut oy = ast.tokens.clone();
for t in &oy { oy.remove(0);
result+=t.value.as_str() if oy.len() > 1 {
} result += "(";
result += "): ("; for t in &oy {
for t in oy { result+=t.value.as_str()
if t.value == "," { }
result+=", " result += "): (";
for t in oy {
if t.value == "," {
result+=", "
} else {
result+=ast.tokens[0].value.as_str()
}
}
result += ")";
} else { } else {
result+=ast.tokens[0].value.as_str() result += format!("let {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
}
} else if ast.ast_type == AstType::MutVariableDeceleration {
let _o = String::new();
let mut oy = ast.tokens.clone();
oy.remove(0);
if oy.len() > 1 {
result += "(";
for t in &oy {
result+=t.value.as_str()
}
result += "): (";
for t in oy {
if t.value == "," {
result+=", "
} else {
result+=ast.tokens[0].value.as_str()
}
}
result += ")";
} else {
result += format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
}
} else {
if ast.tokens[0].token_type==TokenType::Newline {
result += (ast.tokens[0].value.as_str().to_owned() + (" ".repeat((indent as usize)*2).as_str())).as_str()
}
else {
result += ast.tokens[0].value.as_str()
} }
} }
result += ")";
} else {
result += format!("let {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
}
} else if ast.ast_type == AstType::MutVariableDeceleration {
let _o = String::new();
let mut oy = ast.tokens.clone();
oy.remove(0);
if oy.len() > 1 {
result += "(";
for t in &oy {
result+=t.value.as_str()
}
result += "): (";
for t in oy {
if t.value == "," {
result+=", "
} else {
result+=ast.tokens[0].value.as_str()
}
}
result += ")";
} else {
result += format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
}
} else {
if ast.tokens[0].token_type==TokenType::Newline {
result += (ast.tokens[0].value.as_str().to_owned() + (" ".repeat((indent as usize)*2).as_str())).as_str()
}
else {
result += ast.tokens[0].value.as_str()
}
}
}
result = result.trim_end().to_string();
if indent > 0 {
result += "\n";
result += " ".repeat((indent as usize-1)*2).as_str();
return "{".to_owned()+result.as_str()+"}"
} else {
return result
}
},
Err((state, _tokens)) => {
println!("Invalid character at code.ws:{}:{}", state.line, state.column);
return "".to_string();
}
} }
result = result.trim_end().to_string();
if indent > 0 {
result += "\n";
result += " ".repeat((indent as usize-1)*2).as_str();
return "{".to_owned()+result.as_str()+"}"
} else {
return result
}
} }