small changes
This commit is contained in:
@@ -17,3 +17,4 @@ pest = "2"
|
|||||||
pest_derive = "2.1"
|
pest_derive = "2.1"
|
||||||
clap = "3"
|
clap = "3"
|
||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
|
regex = "1.10.5"
|
||||||
|
|||||||
+5
-1
@@ -8,6 +8,7 @@ mod utils;
|
|||||||
|
|
||||||
use clap::{App, Arg};
|
use clap::{App, Arg};
|
||||||
use compiler::{compile_rust, transpile_file};
|
use compiler::{compile_rust, transpile_file};
|
||||||
|
use utils::match_code;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let matches = App::new("Wyst")
|
let matches = App::new("Wyst")
|
||||||
@@ -64,7 +65,10 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if matches.is_present("stdio") {
|
} else if matches.is_present("stdio") {
|
||||||
println!("stdio mode!");
|
println!(
|
||||||
|
"{:?}",
|
||||||
|
match_code("void testme(int x) {}\ntest".to_string(), 1, 3).unwrap()
|
||||||
|
);
|
||||||
} else if matches.is_present("hex") {
|
} else if matches.is_present("hex") {
|
||||||
let ident = matches.value_of("hex").unwrap().to_string();
|
let ident = matches.value_of("hex").unwrap().to_string();
|
||||||
println!("_0x{}", hex::encode(ident));
|
println!("_0x{}", hex::encode(ident));
|
||||||
|
|||||||
+24
-3
@@ -156,7 +156,7 @@ impl Transpiler {
|
|||||||
match pair.as_rule() {
|
match pair.as_rule() {
|
||||||
Rule::func_def => {
|
Rule::func_def => {
|
||||||
res += format!(
|
res += format!(
|
||||||
"pub fn {}({}) -> {} \n{}\n",
|
"pub fn {}({}) -> {} {}\n",
|
||||||
self.transpile(tokens[1].clone()),
|
self.transpile(tokens[1].clone()),
|
||||||
self.transpile_pairs(tokens[2].clone().into_inner())
|
self.transpile_pairs(tokens[2].clone().into_inner())
|
||||||
.as_str()
|
.as_str()
|
||||||
@@ -168,7 +168,7 @@ impl Transpiler {
|
|||||||
}
|
}
|
||||||
Rule::ptr_fn_def => {
|
Rule::ptr_fn_def => {
|
||||||
res += format!(
|
res += format!(
|
||||||
"pub fn {}({}) -> &mut {} \n{}\n",
|
"pub fn {}({}) -> &mut {} {}\n",
|
||||||
self.transpile(tokens[1].clone()),
|
self.transpile(tokens[1].clone()),
|
||||||
self.transpile_pairs(tokens[2].clone().into_inner())
|
self.transpile_pairs(tokens[2].clone().into_inner())
|
||||||
.as_str()
|
.as_str()
|
||||||
@@ -381,8 +381,29 @@ impl Transpiler {
|
|||||||
res += self.transpile_pairs(pair.into_inner()).as_str();
|
res += self.transpile_pairs(pair.into_inner()).as_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Rule::match_name => {
|
||||||
|
let to_match = tokens[0].clone().as_str();
|
||||||
|
let match_id = tokens[1].clone().as_str();
|
||||||
|
res += "\n";
|
||||||
|
res += self.encode_ident(to_match).as_str();
|
||||||
|
res += "<match:";
|
||||||
|
res += match_id;
|
||||||
|
res += ">";
|
||||||
|
}
|
||||||
|
|
||||||
|
Rule::operation => {
|
||||||
|
for i in 0..tokens.len() {
|
||||||
|
let token = tokens[i].clone();
|
||||||
|
if i % 2 == 0 {
|
||||||
|
res += self.transpile(token).as_str();
|
||||||
|
} else {
|
||||||
|
res += token.as_str();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
println!("\x1b[33mUnh: {}\x1b[0m", pair);
|
println!("\x1b[33mNot Handled: {}\x1b[0m", pair);
|
||||||
res += " ";
|
res += " ";
|
||||||
res += pair.as_str();
|
res += pair.as_str();
|
||||||
res += " ";
|
res += " ";
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
use crate::transpiler::{Rule, State, Transpiler, WystParser};
|
use crate::transpiler::{Rule, State, Transpiler, WystParser};
|
||||||
use dirs::home_dir;
|
use dirs::home_dir;
|
||||||
use pest::Parser;
|
use pest::Parser;
|
||||||
|
use rand::random;
|
||||||
|
use regex::Regex;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@@ -182,6 +184,42 @@ impl ModManager {
|
|||||||
problems
|
problems
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub struct MatchError {
|
||||||
|
msg: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn match_code(code: String, line: usize, column: usize) -> Result<String, MatchError> {
|
||||||
|
let re_include = Regex::new(r#"\#\s*include\s*(<|\")([a-zA-Z0-9_.-]*)$"#).unwrap();
|
||||||
|
let re_ident = Regex::new(r#"(\:\:|[_a-zA-Z])(\:\:|[_a-zA-Z0-9])*$"#).unwrap();
|
||||||
|
let mut lines: Vec<&str> = code.split("\n").collect();
|
||||||
|
if lines.len() >= line {
|
||||||
|
let result = &lines[line][..column];
|
||||||
|
if let Some(caps) = re_include.captures(&result) {
|
||||||
|
let match_id: u16 = random();
|
||||||
|
return Ok(format!("<match_file:{}:_{}>", &caps[0], match_id));
|
||||||
|
} else if let Some(caps) = re_ident.captures(&result) {
|
||||||
|
let match_id: u16 = random();
|
||||||
|
let match_line = format!("<match:{}:_{}>", &caps[0], match_id);
|
||||||
|
lines[line] = match_line.as_str();
|
||||||
|
let mut trsp = Transpiler::default();
|
||||||
|
match trsp.transpile_code(lines.join("\n"), 0) {
|
||||||
|
Ok(rs_code) => {
|
||||||
|
return Ok(rs_code);
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
return Err(MatchError {
|
||||||
|
msg: "Syntax error".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(MatchError {
|
||||||
|
msg: "last resort empty".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub const LOGO: &str = r#"
|
pub const LOGO: &str = r#"
|
||||||
|
|
||||||
|
|||||||
+8
-4
@@ -18,6 +18,7 @@ char = { "'" ~ (escape | ANY) ~ "'" }
|
|||||||
bool = @{"true" | "false"}
|
bool = @{"true" | "false"}
|
||||||
keyword = @{"struct"|"return"}
|
keyword = @{"struct"|"return"}
|
||||||
map = { "#" ~ "map" ~ identifier ~ comma ~ identifier }
|
map = { "#" ~ "map" ~ identifier ~ comma ~ identifier }
|
||||||
|
match_name = { "<match:" ~ identifier ~ ":" ~ identifier ~ ">" }
|
||||||
|
|
||||||
// [numbers]
|
// [numbers]
|
||||||
hex = @{ "0" ~ ("x"|"X") ~ ('0'..'9' | 'a'..'f' | 'A'..'F')*}
|
hex = @{ "0" ~ ("x"|"X") ~ ('0'..'9' | 'a'..'f' | 'A'..'F')*}
|
||||||
@@ -51,8 +52,9 @@ refrence = { "&" ~ identifier }
|
|||||||
|
|
||||||
// [Operators]
|
// [Operators]
|
||||||
if_operator = {"&&"|"||"}
|
if_operator = {"&&"|"||"}
|
||||||
operator = {"=="}
|
operator = {"=="|"+"|"-"|"!="|">"|"<"|">="|"<="}
|
||||||
if_expr = {expr ~ (if_operator ~ (("("~if_expr*~")")|expr))*}
|
if_expr = {expr ~ (if_operator ~ (("("~if_expr*~")")|expr))*}
|
||||||
|
operation = {(term ~ operator)+ ~ term}
|
||||||
|
|
||||||
// [Statement]
|
// [Statement]
|
||||||
return_stm = { "return" ~ expr }
|
return_stm = { "return" ~ expr }
|
||||||
@@ -75,11 +77,12 @@ term = {
|
|||||||
bool |
|
bool |
|
||||||
identifier |
|
identifier |
|
||||||
string |
|
string |
|
||||||
refrence
|
refrence |
|
||||||
|
match_name
|
||||||
}
|
}
|
||||||
|
|
||||||
expr = {
|
expr = {
|
||||||
(term ~ operator ~ term)+ |
|
operation |
|
||||||
(term ~ dot ~ identifier ~ round?) |
|
(term ~ dot ~ identifier ~ round?) |
|
||||||
term
|
term
|
||||||
}
|
}
|
||||||
@@ -108,7 +111,8 @@ top_expr = {
|
|||||||
struct_def |
|
struct_def |
|
||||||
enum_def |
|
enum_def |
|
||||||
map |
|
map |
|
||||||
impl_def
|
impl_def |
|
||||||
|
match_name
|
||||||
)*
|
)*
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user