square and comments
This commit is contained in:
+61
-2
@@ -24,6 +24,7 @@ pub enum TokenType {
|
||||
Square,
|
||||
Include,
|
||||
String,
|
||||
Comment,
|
||||
// EOF,
|
||||
}
|
||||
|
||||
@@ -71,7 +72,7 @@ const SYNTAX: [Node; 14] = [
|
||||
},
|
||||
Node {
|
||||
token_type: TokenType::Keyword,
|
||||
token_regex: Lazy::new(|| Regex::new(r"^mut|try|catch|return|fn\b").unwrap())
|
||||
token_regex: Lazy::new(|| Regex::new(r"^(mut|try|catch|return|fn|let)\b").unwrap())
|
||||
},
|
||||
Node {
|
||||
token_type: TokenType::Identifier,
|
||||
@@ -87,7 +88,7 @@ const SYNTAX: [Node; 14] = [
|
||||
},
|
||||
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,
|
||||
@@ -126,6 +127,55 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
|
||||
let fch = get_first_char(code);
|
||||
let brln = brtp.len();
|
||||
match fch.as_str() {
|
||||
"/" => {
|
||||
brstr += "/";
|
||||
code = code.strip_prefix(fch.as_str()).expect("");
|
||||
if brln == 0 {
|
||||
if code.len() > 0 {
|
||||
let sch = get_first_char(code);
|
||||
if sch=="/" {
|
||||
code = code.strip_prefix(sch.as_str()).expect("");
|
||||
brstr += &sch;
|
||||
brtp.push(4);
|
||||
if brln > 0 {
|
||||
brstr += fch.as_str();
|
||||
} else {
|
||||
br_state.line = state.line;
|
||||
br_state.column = state.column;
|
||||
}
|
||||
} else if sch=="*" {
|
||||
code = code.strip_prefix(sch.as_str()).expect("");
|
||||
brstr += &sch;
|
||||
brtp.push(5);
|
||||
if brln > 0 {
|
||||
brstr += fch.as_str();
|
||||
} else {
|
||||
br_state.line = state.line;
|
||||
br_state.column = state.column;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"*" => {
|
||||
brstr += "*";
|
||||
code = code.strip_prefix(fch.as_str()).expect("");
|
||||
if code.len() > 0 {
|
||||
let sch = get_first_char(code);
|
||||
if sch=="/" && brtp[brln-1]==5 {
|
||||
code = code.strip_prefix(sch.as_str()).expect("");
|
||||
brstr += &sch;
|
||||
brtp.pop();
|
||||
tokens.push(Token {
|
||||
token_type: TokenType::Comment,
|
||||
value: brstr.clone(),
|
||||
column: br_state.column,
|
||||
line: br_state.line
|
||||
});
|
||||
brstr = String::new();
|
||||
}
|
||||
}
|
||||
}
|
||||
"\"" => {
|
||||
brstr += "\"";
|
||||
code = code.strip_prefix(fch.as_str()).expect("");
|
||||
@@ -250,6 +300,15 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
|
||||
code = code.strip_prefix(fch.as_str()).expect("");
|
||||
if brln > 0 {
|
||||
brstr += "\n";
|
||||
} if brln==1 && brtp[0]==4 {
|
||||
brtp.pop();
|
||||
tokens.push(Token {
|
||||
token_type: TokenType::Comment,
|
||||
value: brstr.clone(),
|
||||
column: br_state.column,
|
||||
line: br_state.line
|
||||
});
|
||||
brstr = String::new();
|
||||
}
|
||||
state.line += 1;
|
||||
state.column = 0;
|
||||
|
||||
+83
-6
@@ -70,6 +70,15 @@ pub fn transpile(input: String, indent: u32, state: LexerState) -> String {
|
||||
})
|
||||
)
|
||||
.as_str();
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square {
|
||||
result += format!(
|
||||
"[{}]",
|
||||
transpile_square(ast.tokens[0].value.clone(), LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
column: ast.tokens[0].column
|
||||
})
|
||||
)
|
||||
.as_str();
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly {
|
||||
if ast.tokens[0].token_type == TokenType::Newline {
|
||||
result += (ast.tokens[0].value.as_str().to_owned()
|
||||
@@ -78,7 +87,10 @@ pub fn transpile(input: String, indent: u32, state: LexerState) -> String {
|
||||
} else {
|
||||
result += ast.tokens[0].value.as_str();
|
||||
}
|
||||
} else {
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr {
|
||||
result+=".";
|
||||
} // flp
|
||||
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()))
|
||||
@@ -88,6 +100,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState) -> String {
|
||||
result += " ".repeat((indent as usize) * 2).as_str();
|
||||
} else {
|
||||
result += ast.tokens[0].value.as_str();
|
||||
result += " ";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,8 +116,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState) -> String {
|
||||
}
|
||||
}
|
||||
Err((state, _tokens)) => {
|
||||
println!("Invalid syntax at code.ws:{}:{}", state.line, state.column);
|
||||
return "".to_string();
|
||||
panic!("Invalid syntax at code.ws:{}:{}", state.line, state.column);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,7 +148,19 @@ pub fn transpile_round(input: String, state: LexerState) -> String {
|
||||
})
|
||||
)
|
||||
.as_str();
|
||||
} else {
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square {
|
||||
result += format!(
|
||||
"[{}]",
|
||||
transpile_square(ast.tokens[0].value.clone(), LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
column: ast.tokens[0].column
|
||||
})
|
||||
)
|
||||
.as_str();
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr {
|
||||
result+=".";
|
||||
} // flp
|
||||
else {
|
||||
result += ast.tokens[0].value.as_str();
|
||||
result += " ";
|
||||
}
|
||||
@@ -146,8 +170,61 @@ pub fn transpile_round(input: String, state: LexerState) -> String {
|
||||
result
|
||||
}
|
||||
Err((state, _tokens)) => {
|
||||
println!("Invalid syntax at code.ws:{}:{}", state.line, state.column);
|
||||
return "".to_string();
|
||||
panic!("Invalid syntax at code.ws:{}:{}", state.line, state.column);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn transpile_square(input: String, state: LexerState) -> String {
|
||||
let mut result = String::new();
|
||||
let lexer_out = lex(input.as_str(), false, state);
|
||||
|
||||
match lexer_out {
|
||||
Ok(tokens) => {
|
||||
let mut full_ast = Parser::new(tokens.clone());
|
||||
|
||||
while full_ast.tokens.len() > full_ast.index as usize {
|
||||
let ast = full_ast.next();
|
||||
println!("{ast}");
|
||||
|
||||
if ast.ast_type == AstType::VariableDeceleration {
|
||||
result += format!("{}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
|
||||
} else if ast.ast_type == AstType::MutVariableDeceleration {
|
||||
result += format!("mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
|
||||
} else if ast.ast_type == AstType::PointerDeceleration {
|
||||
result += format!("{}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round {
|
||||
result += format!(
|
||||
"({})",
|
||||
transpile_round(ast.tokens[0].value.clone(), LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
column: ast.tokens[0].column
|
||||
})
|
||||
)
|
||||
.as_str();
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square {
|
||||
result += format!(
|
||||
"[{}]",
|
||||
transpile_square(ast.tokens[0].value.clone(), LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
column: ast.tokens[0].column
|
||||
})
|
||||
)
|
||||
.as_str();
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr {
|
||||
result+=".";
|
||||
} // flp
|
||||
else {
|
||||
result += ast.tokens[0].value.as_str();
|
||||
result += " ";
|
||||
}
|
||||
}
|
||||
|
||||
result = result.trim_end().to_string();
|
||||
result
|
||||
}
|
||||
Err((state, _tokens)) => {
|
||||
panic!("Invalid syntax at code.ws:{}:{}", state.line, state.column);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user