Testing simple grammar
This commit is contained in:
@@ -26,3 +26,4 @@ target
|
|||||||
/target
|
/target
|
||||||
|
|
||||||
/test/build
|
/test/build
|
||||||
|
/test/output.json
|
||||||
+27
@@ -30,6 +30,13 @@ fn main() {
|
|||||||
}
|
}
|
||||||
cmd_check(&args[2]);
|
cmd_check(&args[2]);
|
||||||
}
|
}
|
||||||
|
"parse" => {
|
||||||
|
if args.len() < 3 {
|
||||||
|
eprintln!("error: expected a file path\n usage: mist check <file.ms>");
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
cmd_parse(&args[2]);
|
||||||
|
}
|
||||||
"version" | "--version" | "-v" => {
|
"version" | "--version" | "-v" => {
|
||||||
println!("mist {}", env!("CARGO_PKG_VERSION"));
|
println!("mist {}", env!("CARGO_PKG_VERSION"));
|
||||||
}
|
}
|
||||||
@@ -61,6 +68,26 @@ fn cmd_check(path: &str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn cmd_parse(path: &str) {
|
||||||
|
let source = read_ms_file(path);
|
||||||
|
match parser::parse(&source) {
|
||||||
|
Ok(output) => {
|
||||||
|
fs::write(
|
||||||
|
"output.json",
|
||||||
|
serde_json::to_string_pretty(&output).unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap_or_else(|e| {
|
||||||
|
eprintln!("error: could not write output.json: {}", e);
|
||||||
|
process::exit(1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("parse error:\n{}", e);
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn read_ms_file(path: &str) -> String {
|
fn read_ms_file(path: &str) -> String {
|
||||||
let pb = PathBuf::from(path);
|
let pb = PathBuf::from(path);
|
||||||
|
|
||||||
|
|||||||
+10
-3
@@ -1,9 +1,11 @@
|
|||||||
#[derive(Debug, Clone)]
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub enum TypeExpr {
|
pub enum TypeExpr {
|
||||||
Identifier(String),
|
Identifier(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub enum TopLevel {
|
pub enum TopLevel {
|
||||||
Import(String),
|
Import(String),
|
||||||
StructDecl {
|
StructDecl {
|
||||||
@@ -18,15 +20,20 @@ pub enum TopLevel {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub enum Statement {
|
pub enum Statement {
|
||||||
Expression(String),
|
Expression(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub enum Expression {
|
pub enum Expression {
|
||||||
Identifier(String),
|
Identifier(String),
|
||||||
IntLiteral(i64),
|
IntLiteral(i64),
|
||||||
FloatLiteral(f64),
|
FloatLiteral(f64),
|
||||||
BoolLiteral(bool),
|
BoolLiteral(bool),
|
||||||
StringLiteral(String),
|
StringLiteral(String),
|
||||||
|
FunctionCall {
|
||||||
|
callee: Box<Expression>,
|
||||||
|
args: Vec<Expression>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
|
|||||||
COMMENT = _{ "//" ~ (!"\n" ~ ANY)* }
|
COMMENT = _{ "//" ~ (!"\n" ~ ANY)* }
|
||||||
|
|
||||||
program = { SOI ~ item* ~ EOI }
|
program = { SOI ~ item* ~ EOI }
|
||||||
item = _{ import_decl | function_decl | struct_decl }
|
item = _{ import_decl | function_decl }
|
||||||
|
|
||||||
// primitives
|
// primitives
|
||||||
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
|
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
|
||||||
@@ -16,20 +16,15 @@ inner_str = @{ (!"\"" ~ ANY)* }
|
|||||||
|
|
||||||
import_decl = { "import" ~ string_lit ~ ";" }
|
import_decl = { "import" ~ string_lit ~ ";" }
|
||||||
|
|
||||||
struct_field = { identifier ~ ":" ~ type_expr ~ ";" }
|
|
||||||
struct_decl = {
|
|
||||||
"struct" ~ identifier ~ "{" ~ struct_field* ~ "}"
|
|
||||||
}
|
|
||||||
|
|
||||||
param_list = { param ~ ("," ~ param)* ~ ","? }
|
param_list = { param ~ ("," ~ param)* ~ ","? }
|
||||||
param = { identifier ~ ":" ~ type_expr }
|
param = { identifier ~ ":" ~ type_expr }
|
||||||
function_decl = {
|
function_decl = {
|
||||||
"function" ~ identifier ~ "(" ~ param_list? ~ ")" ~ ("->" ~ type_expr)? ~ block
|
"function" ~ identifier ~ "(" ~ param_list? ~ ")" ~ (":" ~ type_expr)? ~ block
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= STATEMENTS =================
|
// ================= STATEMENTS =================
|
||||||
|
|
||||||
block = { "{" ~ statement* ~ "}" }
|
block = { "{" ~ "}" }
|
||||||
|
|
||||||
statement = _{ "idk" }
|
statement = _{ "idk" }
|
||||||
|
|
||||||
|
|||||||
+1
-7
@@ -1,9 +1,3 @@
|
|||||||
import "fmt";
|
import "fmt";
|
||||||
|
|
||||||
function add(a: int, b: int) -> int {
|
function add(a: int, b: int): int {}
|
||||||
return a + b;
|
|
||||||
}
|
|
||||||
|
|
||||||
function main() {
|
|
||||||
fmt.Println(add(10, 10) / 2);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user