Compare commits
74
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b70e862e1a | ||
|
|
056f955fe2 | ||
|
|
9594de08c7 | ||
|
|
5855a6cb5b | ||
|
|
488b985992 | ||
|
|
02c40d7f90 | ||
|
|
195172a293 | ||
|
|
d8543fb3d0 | ||
|
|
b4edb33cbc | ||
|
|
aedf80822c | ||
|
|
872fdf09e6 | ||
|
|
4118e4b562 | ||
|
|
753f8501c0 | ||
|
|
040be1df88 | ||
|
|
4f2d1d3664 | ||
|
|
2576c77466 | ||
|
|
1ee3ae317e | ||
|
|
27a772dad8 | ||
|
|
bd262f98f8 | ||
|
|
22cb5aade3 | ||
|
|
d514fa9efc | ||
|
|
141d2ab7b0 | ||
|
|
ea6aabd896 | ||
|
|
53b87eb02e | ||
|
|
797eed173c | ||
|
|
10a304bfe4 | ||
|
|
842383ba6e | ||
|
|
664e8aba74 | ||
|
|
f5126ad37c | ||
|
|
5b5b6aa795 | ||
|
|
e45546ad2e | ||
|
|
18fedba119 | ||
|
|
508b180d63 | ||
|
|
1822ce7bfb | ||
|
|
a28d431a03 | ||
|
|
4d6e749866 | ||
|
|
101a8c65ac | ||
|
|
60d668a222 | ||
|
|
ec30d18e15 | ||
|
|
d2f7c0c69b | ||
|
|
7996a3ffaa | ||
|
|
d0b7898ad7 | ||
|
|
41f47f3241 | ||
|
|
e90e9a1f29 | ||
|
|
02447bf510 | ||
|
|
1edcdade33 | ||
|
|
2d46afff9a | ||
|
|
3e1fa5cd18 | ||
|
|
819441bc29 | ||
|
|
8ed8eed5de | ||
|
|
d27b607686 | ||
|
|
1babc77957 | ||
|
|
bf3783a615 | ||
|
|
874246f8c6 | ||
|
|
608ca0916c | ||
|
|
78738a5faf | ||
|
|
09c7f07c95 | ||
|
|
04087abbdf | ||
|
|
88bf07b913 | ||
|
|
0d63528d10 | ||
|
|
cb03653e88 | ||
|
|
7ce90478f0 | ||
|
|
4907533441 | ||
|
|
deeb9350b4 | ||
|
|
6717be038f | ||
|
|
19629363b0 | ||
|
|
6df7f672d0 | ||
|
|
5de519f561 | ||
|
|
1b3b98458d | ||
|
|
1847b2f282 | ||
|
|
b0ffd300d9 | ||
|
|
f3875a4ec9 | ||
|
|
98608a1883 | ||
|
|
34b71c17c3 |
Generated
+19
@@ -77,6 +77,16 @@ checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||
[[package]]
|
||||
name = "mist"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"parser",
|
||||
"semantic",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parser"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"pest",
|
||||
"pest_derive",
|
||||
@@ -145,6 +155,15 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semantic"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"parser",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
|
||||
+2
-2
@@ -4,7 +4,7 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
pest = "2.8.6"
|
||||
pest_derive = "2.8.6"
|
||||
serde = { version = "1.0.228", features = ["serde_derive"] }
|
||||
serde_json = "1.0.149"
|
||||
parser = { path = "./parser" }
|
||||
semantic = { path = "./semantic" }
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
module mapgen
|
||||
|
||||
go 1.25.5
|
||||
|
||||
require (
|
||||
golang.org/x/mod v0.35.0 // indirect
|
||||
golang.org/x/sync v0.20.0 // indirect
|
||||
golang.org/x/tools v0.44.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
golang.org/x/mod v0.35.0 h1:Ww1D637e6Pg+Zb2KrWfHQUnH2dQRLBQyAtpr/haaJeM=
|
||||
golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU=
|
||||
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
|
||||
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/tools v0.44.0 h1:UP4ajHPIcuMjT1GqzDWRlalUEoY+uzoZKnhOjbIPD2c=
|
||||
golang.org/x/tools v0.44.0/go.mod h1:KA0AfVErSdxRZIsOVipbv3rQhVXTnlU6UhKxHd1seDI=
|
||||
+188
@@ -0,0 +1,188 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"os"
|
||||
"unicode"
|
||||
|
||||
"golang.org/x/tools/go/packages"
|
||||
)
|
||||
|
||||
type TypeSymbol string
|
||||
|
||||
type VarSymbol struct {
|
||||
Export bool `json:"export"`
|
||||
VarType TypeSymbol `json:"var_type"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type FunctionSymbol struct {
|
||||
Export bool `json:"export"`
|
||||
Name string `json:"name"`
|
||||
Params map[string]VarSymbol `json:"params"`
|
||||
ReturnTypes []TypeSymbol `json:"return_types"`
|
||||
}
|
||||
|
||||
type StructSymbol struct {
|
||||
Export bool `json:"export"`
|
||||
Name string `json:"name"`
|
||||
Fields map[string]VarSymbol `json:"fields"`
|
||||
Methods map[string]FunctionSymbol `json:"methods"`
|
||||
}
|
||||
|
||||
type TopLevelSymbolScope struct {
|
||||
PackageName string `json:"package_name"`
|
||||
Structs map[string]StructSymbol `json:"structs"`
|
||||
Functions map[string]FunctionSymbol `json:"functions"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
cfg := &packages.Config{
|
||||
Mode: packages.NeedName |
|
||||
packages.NeedTypes |
|
||||
packages.NeedTypesInfo |
|
||||
packages.NeedModule,
|
||||
}
|
||||
|
||||
// you can expand this to multiple packages later
|
||||
pkgs, err := packages.Load(cfg, "fmt")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if packages.PrintErrors(pkgs) > 0 {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// 🔑 FINAL RESULT: map[import_path]scope
|
||||
result := make(map[string]TopLevelSymbolScope)
|
||||
|
||||
for _, pkg := range pkgs {
|
||||
scopeData := TopLevelSymbolScope{
|
||||
PackageName: pkg.Name, // e.g. "http"
|
||||
Structs: make(map[string]StructSymbol),
|
||||
Functions: make(map[string]FunctionSymbol),
|
||||
}
|
||||
|
||||
scope := pkg.Types.Scope()
|
||||
|
||||
for _, name := range scope.Names() {
|
||||
if !token.IsExported(name) {
|
||||
continue
|
||||
}
|
||||
|
||||
obj := scope.Lookup(name)
|
||||
|
||||
switch obj := obj.(type) {
|
||||
|
||||
case *types.Func:
|
||||
scopeData.Functions[LowerFirst(name)] = buildFunction(obj)
|
||||
|
||||
case *types.TypeName:
|
||||
if strct, ok := buildStruct(obj); ok {
|
||||
scopeData.Structs[name] = strct
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 🔑 key = import path (e.g. "net/http")
|
||||
result[pkg.PkgPath] = scopeData
|
||||
}
|
||||
|
||||
out, err := json.MarshalIndent(result, "", " ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(out))
|
||||
}
|
||||
|
||||
func LowerFirst(s string) string {
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
r := []rune(s)
|
||||
r[0] = unicode.ToLower(r[0])
|
||||
return string(r)
|
||||
}
|
||||
|
||||
func buildFunction(fn *types.Func) FunctionSymbol {
|
||||
sig := fn.Type().(*types.Signature)
|
||||
|
||||
params := make(map[string]VarSymbol)
|
||||
for i := 0; i < sig.Params().Len(); i++ {
|
||||
p := sig.Params().At(i)
|
||||
|
||||
name := p.Name()
|
||||
if name == "" {
|
||||
name = fmt.Sprintf("param%d", i)
|
||||
}
|
||||
|
||||
params[name] = VarSymbol{
|
||||
Export: true,
|
||||
Name: name,
|
||||
VarType: TypeSymbol(p.Type().String()),
|
||||
}
|
||||
}
|
||||
|
||||
var returnTypes []TypeSymbol
|
||||
for i := 0; i < sig.Results().Len(); i++ {
|
||||
r := sig.Results().At(i)
|
||||
returnTypes = append(returnTypes, TypeSymbol(r.Type().String()))
|
||||
}
|
||||
|
||||
return FunctionSymbol{
|
||||
Export: true, // already filtered before calling
|
||||
Name: fn.Name(),
|
||||
Params: params,
|
||||
ReturnTypes: returnTypes,
|
||||
}
|
||||
}
|
||||
|
||||
func buildStruct(tn *types.TypeName) (StructSymbol, bool) {
|
||||
underlying := tn.Type().Underlying()
|
||||
|
||||
strct, ok := underlying.(*types.Struct)
|
||||
if !ok {
|
||||
return StructSymbol{}, false
|
||||
}
|
||||
|
||||
fields := make(map[string]VarSymbol)
|
||||
|
||||
for i := 0; i < strct.NumFields(); i++ {
|
||||
f := strct.Field(i)
|
||||
|
||||
if !token.IsExported(f.Name()) {
|
||||
continue
|
||||
}
|
||||
|
||||
fields[f.Name()] = VarSymbol{
|
||||
Export: true,
|
||||
Name: f.Name(),
|
||||
VarType: TypeSymbol(f.Type().String()),
|
||||
}
|
||||
}
|
||||
|
||||
methods := make(map[string]FunctionSymbol)
|
||||
methodSet := types.NewMethodSet(tn.Type())
|
||||
|
||||
for i := 0; i < methodSet.Len(); i++ {
|
||||
m := methodSet.At(i).Obj().(*types.Func)
|
||||
|
||||
if !token.IsExported(m.Name()) {
|
||||
continue
|
||||
}
|
||||
|
||||
methods[LowerFirst(m.Name())] = buildFunction(m)
|
||||
}
|
||||
|
||||
return StructSymbol{
|
||||
Export: true,
|
||||
Name: tn.Name(),
|
||||
Fields: fields,
|
||||
Methods: methods,
|
||||
}, true
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "parser"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
pest = "2.8.6"
|
||||
pest_derive = "2.8.6"
|
||||
serde = { version = "1.0.228", features = ["serde_derive"] }
|
||||
serde_json = "1.0.149"
|
||||
@@ -1,13 +1,14 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ParamList(pub Vec<(String, TypeExpr)>);
|
||||
pub struct ParamList(pub HashMap<String, (bool, TypeExpr)>);
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct Block(pub Vec<Statement>);
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum TypeExpr {
|
||||
Identifier(String),
|
||||
}
|
||||
@@ -28,9 +29,9 @@ pub enum BinaryOp {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum TopLevel {
|
||||
Import(String),
|
||||
Package(String),
|
||||
StructDecl {
|
||||
export: bool,
|
||||
name: String,
|
||||
@@ -46,16 +47,15 @@ pub enum TopLevel {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum Postfix {
|
||||
FieldAccess(String),
|
||||
Call(Vec<Expression>),
|
||||
StructCall(HashMap<String, Expression>),
|
||||
Index(Expression),
|
||||
Binary(BinaryOp, Expression),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum Statement {
|
||||
// expr;
|
||||
Expression(Expression),
|
||||
@@ -68,6 +68,7 @@ pub enum Statement {
|
||||
kind: VarKind,
|
||||
name: String,
|
||||
init: Option<Expression>,
|
||||
type_: Option<TypeExpr>,
|
||||
},
|
||||
|
||||
VarAssign {
|
||||
@@ -104,7 +105,6 @@ pub enum Statement {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum Expression {
|
||||
Identifier(String),
|
||||
IntLiteral(i64),
|
||||
@@ -0,0 +1,123 @@
|
||||
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
|
||||
COMMENT = _{ "//" ~ (!"\n" ~ ANY)* }
|
||||
|
||||
program = { SOI ~ top_level* ~ EOI }
|
||||
|
||||
// primitives
|
||||
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
|
||||
integer = @{ ASCII_DIGIT+ }
|
||||
float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }
|
||||
boolean = { "true" | "false" }
|
||||
string_lit = { "\"" ~ inner_str ~ "\"" }
|
||||
inner_str = @{ (!"\"" ~ ANY)* }
|
||||
|
||||
// ================= TOP LEVEL =================
|
||||
|
||||
top_level = _{ import | function_decl | struct_decl | package }
|
||||
|
||||
package = { "package" ~ identifier ~ ";" }
|
||||
|
||||
export = { "public" }
|
||||
|
||||
import = { "import" ~ string_lit ~ ";" }
|
||||
|
||||
param_list = { param ~ ("," ~ param)* ~ ","? }
|
||||
param = { export? ~ type_expr ~ identifier }
|
||||
|
||||
struct_decl = {
|
||||
export? ~ "struct" ~ identifier ~ "{" ~ param_list? ~ "}"
|
||||
}
|
||||
|
||||
function_decl = {
|
||||
export? ~ type_expr ~ identifier ~ "(" ~ param_list? ~ ")" ~ block
|
||||
}
|
||||
|
||||
// ================= STATEMENTS =================
|
||||
|
||||
block = { "{" ~ statement_list ~ "}" }
|
||||
|
||||
statement_list = { statement* }
|
||||
|
||||
statement = _{
|
||||
expr_stmt
|
||||
| var_decl
|
||||
| var_assign
|
||||
| if_stmt
|
||||
| while_stmt
|
||||
| for_stmt
|
||||
| return_stmt
|
||||
| break_stmt
|
||||
| continue_stmt
|
||||
| block
|
||||
}
|
||||
|
||||
// --- basic statements ---
|
||||
|
||||
expr_stmt = { expr ~ ";" }
|
||||
|
||||
return_stmt = { "return" ~ expr? ~ ";" }
|
||||
|
||||
break_stmt = { "break" ~ ";" }
|
||||
|
||||
continue_stmt = { "continue" ~ ";" }
|
||||
|
||||
// --- variable declarations ---
|
||||
|
||||
// var_decl_kind = { "let" | "const" | "var" }
|
||||
|
||||
var_decl = {
|
||||
type_expr ~ identifier ~ ("=" ~ expr)? ~ ";"
|
||||
}
|
||||
|
||||
var_assign = {
|
||||
expr ~ "=" ~ expr ~ ";"
|
||||
}
|
||||
|
||||
// --- control flow ---
|
||||
|
||||
if_stmt = {
|
||||
"if" ~ "(" ~ expr ~ ")" ~ statement ~ ("else" ~ statement)?
|
||||
}
|
||||
|
||||
while_stmt = {
|
||||
"while" ~ "(" ~ expr ~ ")" ~ statement
|
||||
}
|
||||
|
||||
for_stmt = {
|
||||
"for" ~ "(" ~ var_decl ~ expr ~ ";" ~ var_assign_no_semicolon ~ ")" ~ statement
|
||||
}
|
||||
|
||||
var_assign_no_semicolon = {
|
||||
expr ~ "=" ~ expr
|
||||
}
|
||||
|
||||
// ================= EXPRESSIONS =================
|
||||
type_expr = { identifier }
|
||||
|
||||
primary = {
|
||||
integer
|
||||
| float
|
||||
| boolean
|
||||
| string_lit
|
||||
| identifier
|
||||
}
|
||||
|
||||
postfix = {
|
||||
field_px
|
||||
| call_px
|
||||
| struct_px
|
||||
| index_px
|
||||
| binary_px
|
||||
}
|
||||
|
||||
field_px = { "." ~ identifier }
|
||||
call_px = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" }
|
||||
struct_px = { "{" ~ (struct_field_decl ~ ("," ~ struct_field_decl)*)? ~ "}" }
|
||||
index_px = { "[" ~ expr ~ "]" }
|
||||
binary_px = { bin_op ~ expr }
|
||||
bin_op = { "+" | "-" | "*" | "/" | "%" | "==" | "!=" | "<" | ">" | "<=" | ">=" }
|
||||
struct_field_decl = { identifier ~ ":" ~ expr }
|
||||
|
||||
expr = {
|
||||
primary ~ postfix*
|
||||
}
|
||||
@@ -0,0 +1,403 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use pest::Parser;
|
||||
use pest_derive::Parser;
|
||||
|
||||
pub mod ast;
|
||||
pub mod script_parser;
|
||||
|
||||
use ast::*;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[grammar = "./src/grammar.pest"]
|
||||
pub struct MistParser;
|
||||
|
||||
// convenience alias for pest errors
|
||||
pub type ParseError = pest::error::Error<Rule>;
|
||||
|
||||
pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> {
|
||||
let mut pairs = MistParser::parse(Rule::program, source)?;
|
||||
|
||||
let mut statements = vec![];
|
||||
|
||||
for pair in pairs.next().unwrap().into_inner() {
|
||||
if let Ok(stmt) = TopLevel::try_from(pair) {
|
||||
statements.push(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(statements)
|
||||
}
|
||||
|
||||
impl TryFrom<pest::iterators::Pair<'_, Rule>> for TypeExpr {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'_, Rule>) -> Result<Self, Self::Error> {
|
||||
if pair.as_str() == "void" {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
Ok(match pair.as_rule() {
|
||||
Rule::type_expr => {
|
||||
let inner = pair.into_inner().next().unwrap();
|
||||
TypeExpr::try_from(inner)?
|
||||
}
|
||||
Rule::identifier => TypeExpr::Identifier(pair.as_str().to_string()),
|
||||
_ => unimplemented!("{pair:#?}"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(bool, pest::iterators::Pair<'_, Rule>)> for ParamList {
|
||||
fn from((export_allowed, pair): (bool, pest::iterators::Pair<Rule>)) -> Self {
|
||||
let params = pair
|
||||
.into_inner()
|
||||
.map(|p| {
|
||||
let mut param_inner = p.into_inner();
|
||||
let export =
|
||||
if export_allowed && param_inner.peek().unwrap().as_rule() == Rule::export {
|
||||
param_inner.next().unwrap();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let param_type = TypeExpr::try_from(param_inner.next().unwrap()).unwrap();
|
||||
let param_name = param_inner.next().unwrap().as_str().to_string();
|
||||
(param_name, (export, param_type))
|
||||
})
|
||||
.collect();
|
||||
|
||||
ParamList(params)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
||||
type Error = ();
|
||||
fn try_from(pair: pest::iterators::Pair<Rule>) -> Result<Self, ()> {
|
||||
match pair.as_rule() {
|
||||
Rule::import => {
|
||||
let path = pair.into_inner().next().unwrap().as_str().to_string();
|
||||
Ok(TopLevel::Import(path))
|
||||
}
|
||||
Rule::package => {
|
||||
let name = pair.into_inner().next().unwrap().as_str().to_string();
|
||||
Ok(TopLevel::Package(name))
|
||||
}
|
||||
Rule::function_decl => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let export = if let Some(first) = inner.peek() {
|
||||
if first.as_rule() == Rule::export {
|
||||
inner.next();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let return_type = if let Some(next) = inner.peek() {
|
||||
if next.as_rule() == Rule::type_expr {
|
||||
TypeExpr::try_from(inner.next().unwrap()).ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let name = inner.next().unwrap().as_str().to_string();
|
||||
let params = if inner.peek().unwrap().as_rule() == Rule::param_list {
|
||||
ParamList::from((false, inner.next().unwrap()))
|
||||
} else {
|
||||
ParamList(HashMap::new())
|
||||
};
|
||||
|
||||
let body = Block::from(inner.next().unwrap());
|
||||
|
||||
Ok(TopLevel::FunctionDecl {
|
||||
export,
|
||||
name,
|
||||
params,
|
||||
return_type,
|
||||
body,
|
||||
})
|
||||
}
|
||||
|
||||
Rule::struct_decl => {
|
||||
let mut inner = pair.into_inner();
|
||||
let export = if let Some(first) = inner.peek() {
|
||||
if first.as_rule() == Rule::export {
|
||||
inner.next();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let name = inner.next().unwrap().as_str().to_string();
|
||||
let fields_pair = inner.next().unwrap();
|
||||
let fields = ParamList::from((true, fields_pair));
|
||||
|
||||
Ok(TopLevel::StructDecl {
|
||||
export,
|
||||
name,
|
||||
fields,
|
||||
})
|
||||
}
|
||||
|
||||
Rule::EOI => Err(()),
|
||||
_ => unimplemented!("TopLevel parsing not implemented yet {:?}", pair.as_rule()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for Block {
|
||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
let statements = pair
|
||||
.into_inner()
|
||||
.flat_map(|pair| {
|
||||
if pair.as_rule() == Rule::statement_list {
|
||||
pair.into_inner().map(Statement::from).collect()
|
||||
} else {
|
||||
vec![Statement::from(pair)]
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Block(statements)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
match pair.as_rule() {
|
||||
Rule::statement => {
|
||||
let inner = pair.into_inner().next().unwrap();
|
||||
Statement::from(inner)
|
||||
}
|
||||
|
||||
Rule::expr_stmt => {
|
||||
let expr_pair = pair.into_inner().next().unwrap();
|
||||
Statement::Expression(Expression::from(expr_pair))
|
||||
}
|
||||
|
||||
Rule::block => Statement::Block(Block::from(pair.into_inner().next().unwrap())),
|
||||
|
||||
Rule::var_decl => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let type_ = inner.next().map(TypeExpr::try_from).unwrap().ok();
|
||||
let name = inner.next().unwrap().as_str().to_string();
|
||||
let init = inner.next().map(Expression::from);
|
||||
|
||||
Statement::VarDecl {
|
||||
kind: VarKind::Var,
|
||||
name: name.as_str().to_string(),
|
||||
init,
|
||||
type_,
|
||||
}
|
||||
}
|
||||
|
||||
Rule::return_stmt => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let expr = inner.next().map(Expression::from);
|
||||
|
||||
Statement::Return(expr)
|
||||
}
|
||||
|
||||
Rule::break_stmt => Statement::Break,
|
||||
|
||||
Rule::continue_stmt => Statement::Continue,
|
||||
|
||||
Rule::if_stmt => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let condition = Expression::from(inner.next().unwrap());
|
||||
let then_branch = Statement::from(inner.next().unwrap());
|
||||
|
||||
let else_branch = inner.next().map(Statement::from);
|
||||
|
||||
Statement::If {
|
||||
condition,
|
||||
then_branch: Box::new(then_branch),
|
||||
else_branch: else_branch.map(Box::new),
|
||||
}
|
||||
}
|
||||
|
||||
Rule::while_stmt => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let condition = Expression::from(inner.next().unwrap());
|
||||
let body = Statement::from(inner.next().unwrap());
|
||||
|
||||
Statement::While {
|
||||
condition,
|
||||
body: Box::new(body),
|
||||
}
|
||||
}
|
||||
|
||||
Rule::for_stmt => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let init = inner
|
||||
.next()
|
||||
.map(|p| match p.as_rule() {
|
||||
Rule::var_decl => {
|
||||
let mut it = p.into_inner();
|
||||
|
||||
let kind = match it.next().unwrap().as_str() {
|
||||
"let" => VarKind::Let,
|
||||
"const" => VarKind::Const,
|
||||
"var" => VarKind::Var,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let name = it.next().unwrap().as_str().to_string();
|
||||
let init_expr = it
|
||||
.next()
|
||||
.map(|e| Expression::from(e.into_inner().next().unwrap()));
|
||||
|
||||
(kind, name, init_expr)
|
||||
}
|
||||
_ => unimplemented!(
|
||||
"For loop init parsing not implemented yet: {:?}",
|
||||
p.as_rule()
|
||||
),
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let condition = inner.next().map(Expression::from);
|
||||
let update = inner.next().map(parse_var_assign_no_semicolon);
|
||||
let body = Statement::from(inner.next().unwrap());
|
||||
|
||||
Statement::For {
|
||||
init,
|
||||
condition,
|
||||
update: update.map(Box::new),
|
||||
body: Box::new(body),
|
||||
}
|
||||
}
|
||||
|
||||
Rule::var_assign => {
|
||||
let mut inner = pair.into_inner();
|
||||
let target = Expression::from(inner.next().unwrap());
|
||||
let value = Expression::from(inner.next().unwrap());
|
||||
|
||||
Statement::VarAssign { target, value }
|
||||
}
|
||||
|
||||
_ => unimplemented!(
|
||||
"Statement parsing not implemented yet: {:?}",
|
||||
pair.as_rule()
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for Expression {
|
||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
match pair.as_rule() {
|
||||
Rule::expr => {
|
||||
let mut inner = pair.into_inner();
|
||||
let exp = Expression::from(inner.next().unwrap());
|
||||
|
||||
if inner.len() > 0 {
|
||||
Expression::Postfix {
|
||||
initial: Box::new(exp),
|
||||
postfixes: inner.map(|p| Postfix::from(p)).collect(),
|
||||
}
|
||||
} else {
|
||||
exp
|
||||
}
|
||||
}
|
||||
Rule::primary => Expression::from(pair.into_inner().next().unwrap()),
|
||||
Rule::identifier => Expression::Identifier(pair.as_str().to_string()),
|
||||
Rule::integer => {
|
||||
let value = pair.as_str().parse::<i64>().unwrap();
|
||||
Expression::IntLiteral(value)
|
||||
}
|
||||
Rule::float => {
|
||||
let value = pair.as_str().parse::<f64>().unwrap();
|
||||
Expression::FloatLiteral(value)
|
||||
}
|
||||
Rule::boolean => {
|
||||
let value = pair.as_str().parse::<bool>().unwrap();
|
||||
Expression::BoolLiteral(value)
|
||||
}
|
||||
Rule::string_lit => {
|
||||
let inner_str = pair.into_inner().next().unwrap().as_str();
|
||||
Expression::StringLiteral(inner_str.to_string())
|
||||
}
|
||||
|
||||
_ => unimplemented!(
|
||||
"Expression parsing not implemented yet {:?}",
|
||||
pair.as_rule()
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for Postfix {
|
||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
match pair.as_rule() {
|
||||
Rule::postfix => Postfix::from(pair.into_inner().next().unwrap()),
|
||||
|
||||
Rule::field_px => {
|
||||
let field_name = pair.into_inner().next().unwrap().as_str().to_string();
|
||||
Postfix::FieldAccess(field_name)
|
||||
}
|
||||
|
||||
Rule::call_px => Postfix::Call(pair.into_inner().map(Expression::from).collect()),
|
||||
|
||||
Rule::struct_px => Postfix::StructCall(
|
||||
pair.into_inner()
|
||||
.map(|p| {
|
||||
let mut pi = p.into_inner();
|
||||
(
|
||||
pi.next().unwrap().as_str().to_string(),
|
||||
Expression::from(pi.next().unwrap()),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
|
||||
Rule::index_px => Postfix::Index(Expression::from(pair.into_inner().next().unwrap())),
|
||||
|
||||
Rule::binary_px => {
|
||||
let mut inner = pair.into_inner();
|
||||
let op_pair = inner.next().unwrap();
|
||||
let op = match op_pair.as_str() {
|
||||
"+" => BinaryOp::Plus,
|
||||
"-" => BinaryOp::Minus,
|
||||
"*" => BinaryOp::Multiply,
|
||||
"/" => BinaryOp::Divide,
|
||||
"%" => BinaryOp::Modulo,
|
||||
"==" => BinaryOp::Equal,
|
||||
"!=" => BinaryOp::NotEqual,
|
||||
"<" => BinaryOp::LessThan,
|
||||
">" => BinaryOp::GreaterThan,
|
||||
"<=" => BinaryOp::LessThanOrEqual,
|
||||
">=" => BinaryOp::GreaterThanOrEqual,
|
||||
|
||||
_ => {
|
||||
unimplemented!("Binary operator not implemented yet: {}", op_pair.as_str())
|
||||
}
|
||||
};
|
||||
Postfix::Binary(op, Expression::from(inner.next().unwrap()))
|
||||
}
|
||||
|
||||
_ => unimplemented!("Postfix parsing not implemented yet {:?}", pair.as_rule()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_var_assign_no_semicolon(pair: pest::iterators::Pair<Rule>) -> Statement {
|
||||
let mut inner = pair.into_inner();
|
||||
let target = Expression::from(inner.next().unwrap());
|
||||
let value = Expression::from(inner.next().unwrap());
|
||||
|
||||
Statement::VarAssign { target, value }
|
||||
}
|
||||
@@ -13,14 +13,16 @@ inner_str = @{ (!"\"" ~ ANY)* }
|
||||
|
||||
// ================= TOP LEVEL =================
|
||||
|
||||
top_level = _{ import | function_decl | struct_decl }
|
||||
top_level = _{ import | function_decl | struct_decl | package }
|
||||
|
||||
package = { "package" ~ identifier ~ ";" }
|
||||
|
||||
export = { "export" }
|
||||
|
||||
import = { "import" ~ string_lit ~ ";" }
|
||||
|
||||
param_list = { param ~ ("," ~ param)* ~ ","? }
|
||||
param = { identifier ~ ":" ~ type_expr }
|
||||
param = { export? ~ identifier ~ ":" ~ type_expr }
|
||||
|
||||
struct_decl = {
|
||||
export? ~ "struct" ~ identifier ~ "{" ~ param_list? ~ "}"
|
||||
@@ -103,15 +105,18 @@ primary = {
|
||||
postfix = {
|
||||
field_px
|
||||
| call_px
|
||||
| struct_px
|
||||
| index_px
|
||||
| binary_px
|
||||
}
|
||||
|
||||
field_px = { "." ~ identifier }
|
||||
call_px = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" }
|
||||
struct_px = { "{" ~ (struct_field_decl ~ ("," ~ struct_field_decl)*)? ~ "}" }
|
||||
index_px = { "[" ~ expr ~ "]" }
|
||||
binary_px = { bin_op ~ expr }
|
||||
bin_op = { "+" | "-" | "*" | "/" | "%" | "==" | "!=" | "<" | ">" | "<=" | ">=" }
|
||||
struct_field_decl = { identifier ~ ":" ~ expr }
|
||||
|
||||
expr = {
|
||||
primary ~ postfix*
|
||||
@@ -1,23 +1,24 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use pest::Parser;
|
||||
use pest_derive::Parser;
|
||||
|
||||
pub mod ast;
|
||||
use ast::*;
|
||||
use crate::ast::*;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[grammar = "./src/parser/grammar.pest"]
|
||||
pub struct MistParser;
|
||||
#[grammar = "./src/script_grammar.pest"]
|
||||
pub struct MistScriptParser;
|
||||
|
||||
// convenience alias for pest errors
|
||||
pub type ParseError = pest::error::Error<Rule>;
|
||||
|
||||
pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> {
|
||||
let mut pairs = MistParser::parse(Rule::program, source)?;
|
||||
let mut pairs = MistScriptParser::parse(Rule::program, source)?;
|
||||
|
||||
let mut statements = vec![];
|
||||
|
||||
for pair in pairs.next().unwrap().into_inner() {
|
||||
if let Some(stmt) = TopLevel::from_pair(pair) {
|
||||
if let Ok(stmt) = TopLevel::try_from(pair) {
|
||||
statements.push(stmt);
|
||||
}
|
||||
}
|
||||
@@ -25,12 +26,12 @@ pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> {
|
||||
Ok(statements)
|
||||
}
|
||||
|
||||
impl TypeExpr {
|
||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr {
|
||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
match pair.as_rule() {
|
||||
Rule::type_expr => {
|
||||
let inner = pair.into_inner().next().unwrap();
|
||||
TypeExpr::from_pair(inner)
|
||||
TypeExpr::from(inner)
|
||||
}
|
||||
Rule::identifier => TypeExpr::Identifier(pair.as_str().to_string()),
|
||||
_ => unimplemented!("TypeExpr parsing not implemented yet"),
|
||||
@@ -38,15 +39,22 @@ impl TypeExpr {
|
||||
}
|
||||
}
|
||||
|
||||
impl ParamList {
|
||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
impl From<(bool, pest::iterators::Pair<'_, Rule>)> for ParamList {
|
||||
fn from((export_allowed, pair): (bool, pest::iterators::Pair<Rule>)) -> Self {
|
||||
let params = pair
|
||||
.into_inner()
|
||||
.map(|p| {
|
||||
let mut param_inner = p.into_inner();
|
||||
let export =
|
||||
if export_allowed && param_inner.peek().unwrap().as_rule() == Rule::export {
|
||||
param_inner.next().unwrap();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let param_name = param_inner.next().unwrap().as_str().to_string();
|
||||
let param_type = TypeExpr::from_pair(param_inner.next().unwrap());
|
||||
(param_name, param_type)
|
||||
let param_type = TypeExpr::from(param_inner.next().unwrap());
|
||||
(param_name, (export, param_type))
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -54,12 +62,17 @@ impl ParamList {
|
||||
}
|
||||
}
|
||||
|
||||
impl TopLevel {
|
||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Option<Self> {
|
||||
impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
||||
type Error = ();
|
||||
fn try_from(pair: pest::iterators::Pair<Rule>) -> Result<Self, ()> {
|
||||
match pair.as_rule() {
|
||||
Rule::import => {
|
||||
let path = pair.into_inner().next().unwrap().as_str().to_string();
|
||||
Some(TopLevel::Import(path))
|
||||
Ok(TopLevel::Import(path))
|
||||
}
|
||||
Rule::package => {
|
||||
let name = pair.into_inner().next().unwrap().as_str().to_string();
|
||||
Ok(TopLevel::Package(name))
|
||||
}
|
||||
Rule::function_decl => {
|
||||
let mut inner = pair.into_inner();
|
||||
@@ -75,15 +88,14 @@ impl TopLevel {
|
||||
false
|
||||
};
|
||||
let name = inner.next().unwrap().as_str().to_string();
|
||||
let params_pair = inner.next().unwrap();
|
||||
let params = if params_pair.as_rule() == Rule::param_list {
|
||||
ParamList::from_pair(params_pair)
|
||||
let params = if inner.peek().unwrap().as_rule() == Rule::param_list {
|
||||
ParamList::from((false, inner.next().unwrap()))
|
||||
} else {
|
||||
ParamList(vec![])
|
||||
ParamList(HashMap::new())
|
||||
};
|
||||
let return_type = if let Some(next) = inner.peek() {
|
||||
if next.as_rule() == Rule::type_expr {
|
||||
Some(TypeExpr::from_pair(inner.next().unwrap()))
|
||||
Some(TypeExpr::from(inner.next().unwrap()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -91,9 +103,9 @@ impl TopLevel {
|
||||
None
|
||||
};
|
||||
|
||||
let body = Block::from_pair(inner.next().unwrap());
|
||||
let body = Block::from(inner.next().unwrap());
|
||||
|
||||
Some(TopLevel::FunctionDecl {
|
||||
Ok(TopLevel::FunctionDecl {
|
||||
export,
|
||||
name,
|
||||
params,
|
||||
@@ -116,30 +128,30 @@ impl TopLevel {
|
||||
};
|
||||
let name = inner.next().unwrap().as_str().to_string();
|
||||
let fields_pair = inner.next().unwrap();
|
||||
let fields = ParamList::from_pair(fields_pair);
|
||||
let fields = ParamList::from((true, fields_pair));
|
||||
|
||||
Some(TopLevel::StructDecl {
|
||||
Ok(TopLevel::StructDecl {
|
||||
export,
|
||||
name,
|
||||
fields,
|
||||
})
|
||||
}
|
||||
|
||||
Rule::EOI => None,
|
||||
Rule::EOI => Err(()),
|
||||
_ => unimplemented!("TopLevel parsing not implemented yet {:?}", pair.as_rule()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Block {
|
||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for Block {
|
||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
let statements = pair
|
||||
.into_inner()
|
||||
.flat_map(|pair| {
|
||||
if pair.as_rule() == Rule::statement_list {
|
||||
pair.into_inner().map(Statement::from_pair).collect()
|
||||
pair.into_inner().map(Statement::from).collect()
|
||||
} else {
|
||||
vec![Statement::from_pair(pair)]
|
||||
vec![Statement::from(pair)]
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
@@ -147,20 +159,20 @@ impl Block {
|
||||
}
|
||||
}
|
||||
|
||||
impl Statement {
|
||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
match pair.as_rule() {
|
||||
Rule::statement => {
|
||||
let inner = pair.into_inner().next().unwrap();
|
||||
Statement::from_pair(inner)
|
||||
Statement::from(inner)
|
||||
}
|
||||
|
||||
Rule::expr_stmt => {
|
||||
let expr_pair = pair.into_inner().next().unwrap();
|
||||
Statement::Expression(Expression::from_pair(expr_pair))
|
||||
Statement::Expression(Expression::from(expr_pair))
|
||||
}
|
||||
|
||||
Rule::block => Statement::Block(Block::from_pair(pair.into_inner().next().unwrap())),
|
||||
Rule::block => Statement::Block(Block::from(pair.into_inner().next().unwrap())),
|
||||
|
||||
Rule::var_decl => {
|
||||
let mut inner = pair.into_inner();
|
||||
@@ -168,10 +180,7 @@ impl Statement {
|
||||
let kind_pair = inner.next().unwrap(); // let/const/var
|
||||
let name_pair = inner.next().unwrap(); // identifier
|
||||
|
||||
let init = inner.next().map(|expr_pair| {
|
||||
// expects "=" expr
|
||||
Expression::from_pair(expr_pair.into_inner().next().unwrap())
|
||||
});
|
||||
let init = inner.next().map(Expression::from);
|
||||
|
||||
let kind = match kind_pair.as_str() {
|
||||
"let" => VarKind::Let,
|
||||
@@ -184,13 +193,14 @@ impl Statement {
|
||||
kind,
|
||||
name: name_pair.as_str().to_string(),
|
||||
init,
|
||||
type_: None,
|
||||
}
|
||||
}
|
||||
|
||||
Rule::return_stmt => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let expr = inner.next().map(Expression::from_pair);
|
||||
let expr = inner.next().map(Expression::from);
|
||||
|
||||
Statement::Return(expr)
|
||||
}
|
||||
@@ -202,10 +212,10 @@ impl Statement {
|
||||
Rule::if_stmt => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let condition = Expression::from_pair(inner.next().unwrap());
|
||||
let then_branch = Statement::from_pair(inner.next().unwrap());
|
||||
let condition = Expression::from(inner.next().unwrap());
|
||||
let then_branch = Statement::from(inner.next().unwrap());
|
||||
|
||||
let else_branch = inner.next().map(Statement::from_pair);
|
||||
let else_branch = inner.next().map(Statement::from);
|
||||
|
||||
Statement::If {
|
||||
condition,
|
||||
@@ -217,8 +227,8 @@ impl Statement {
|
||||
Rule::while_stmt => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let condition = Expression::from_pair(inner.next().unwrap());
|
||||
let body = Statement::from_pair(inner.next().unwrap());
|
||||
let condition = Expression::from(inner.next().unwrap());
|
||||
let body = Statement::from(inner.next().unwrap());
|
||||
|
||||
Statement::While {
|
||||
condition,
|
||||
@@ -245,7 +255,7 @@ impl Statement {
|
||||
let name = it.next().unwrap().as_str().to_string();
|
||||
let init_expr = it
|
||||
.next()
|
||||
.map(|e| Expression::from_pair(e.into_inner().next().unwrap()));
|
||||
.map(|e| Expression::from(e.into_inner().next().unwrap()));
|
||||
|
||||
(kind, name, init_expr)
|
||||
}
|
||||
@@ -256,9 +266,9 @@ impl Statement {
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let condition = inner.next().map(Expression::from_pair);
|
||||
let condition = inner.next().map(Expression::from);
|
||||
let update = inner.next().map(parse_var_assign_no_semicolon);
|
||||
let body = Statement::from_pair(inner.next().unwrap());
|
||||
let body = Statement::from(inner.next().unwrap());
|
||||
|
||||
Statement::For {
|
||||
init,
|
||||
@@ -270,8 +280,8 @@ impl Statement {
|
||||
|
||||
Rule::var_assign => {
|
||||
let mut inner = pair.into_inner();
|
||||
let target = Expression::from_pair(inner.next().unwrap());
|
||||
let value = Expression::from_pair(inner.next().unwrap());
|
||||
let target = Expression::from(inner.next().unwrap());
|
||||
let value = Expression::from(inner.next().unwrap());
|
||||
|
||||
Statement::VarAssign { target, value }
|
||||
}
|
||||
@@ -284,23 +294,23 @@ impl Statement {
|
||||
}
|
||||
}
|
||||
|
||||
impl Expression {
|
||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for Expression {
|
||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
match pair.as_rule() {
|
||||
Rule::expr => {
|
||||
let mut inner = pair.into_inner();
|
||||
let exp = Expression::from_pair(inner.next().unwrap());
|
||||
let exp = Expression::from(inner.next().unwrap());
|
||||
|
||||
if inner.len() > 0 {
|
||||
Expression::Postfix {
|
||||
initial: Box::new(exp),
|
||||
postfixes: inner.map(|p| Postfix::from_pair(p)).collect(),
|
||||
postfixes: inner.map(|p| Postfix::from(p)).collect(),
|
||||
}
|
||||
} else {
|
||||
exp
|
||||
}
|
||||
}
|
||||
Rule::primary => Expression::from_pair(pair.into_inner().next().unwrap()),
|
||||
Rule::primary => Expression::from(pair.into_inner().next().unwrap()),
|
||||
Rule::identifier => Expression::Identifier(pair.as_str().to_string()),
|
||||
Rule::integer => {
|
||||
let value = pair.as_str().parse::<i64>().unwrap();
|
||||
@@ -327,21 +337,19 @@ impl Expression {
|
||||
}
|
||||
}
|
||||
|
||||
impl Postfix {
|
||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for Postfix {
|
||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
match pair.as_rule() {
|
||||
Rule::postfix => Postfix::from_pair(pair.into_inner().next().unwrap()),
|
||||
Rule::postfix => Postfix::from(pair.into_inner().next().unwrap()),
|
||||
|
||||
Rule::field_px => {
|
||||
let field_name = pair.into_inner().next().unwrap().as_str().to_string();
|
||||
Postfix::FieldAccess(field_name)
|
||||
}
|
||||
|
||||
Rule::call_px => Postfix::Call(pair.into_inner().map(Expression::from_pair).collect()),
|
||||
Rule::call_px => Postfix::Call(pair.into_inner().map(Expression::from).collect()),
|
||||
|
||||
Rule::index_px => {
|
||||
Postfix::Index(Expression::from_pair(pair.into_inner().next().unwrap()))
|
||||
}
|
||||
Rule::index_px => Postfix::Index(Expression::from(pair.into_inner().next().unwrap())),
|
||||
|
||||
Rule::binary_px => {
|
||||
let mut inner = pair.into_inner();
|
||||
@@ -363,7 +371,7 @@ impl Postfix {
|
||||
unimplemented!("Binary operator not implemented yet: {}", op_pair.as_str())
|
||||
}
|
||||
};
|
||||
Postfix::Binary(op, Expression::from_pair(inner.next().unwrap()))
|
||||
Postfix::Binary(op, Expression::from(inner.next().unwrap()))
|
||||
}
|
||||
|
||||
_ => unimplemented!("Postfix parsing not implemented yet {:?}", pair.as_rule()),
|
||||
@@ -373,8 +381,8 @@ impl Postfix {
|
||||
|
||||
fn parse_var_assign_no_semicolon(pair: pest::iterators::Pair<Rule>) -> Statement {
|
||||
let mut inner = pair.into_inner();
|
||||
let target = Expression::from_pair(inner.next().unwrap());
|
||||
let value = Expression::from_pair(inner.next().unwrap());
|
||||
let target = Expression::from(inner.next().unwrap());
|
||||
let value = Expression::from(inner.next().unwrap());
|
||||
|
||||
Statement::VarAssign { target, value }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "semantic"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
parser = { path = "../parser" }
|
||||
serde = { version = "1.0.228", features = ["serde_derive"] }
|
||||
serde_json = "1.0.149"
|
||||
@@ -0,0 +1,318 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{
|
||||
Arc,
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
},
|
||||
};
|
||||
|
||||
use crate::top_level::{
|
||||
FunctionSymbol, JSONScope, StructSymbol, TopLevelSymbolScope, TypeSymbol, VarSymbol,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum TypeRef {
|
||||
Struct(StructRef),
|
||||
Function(FunctionRef),
|
||||
Package(PackageRef),
|
||||
Name(String),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PackageRef {
|
||||
pub name: String,
|
||||
pub variables: HashMap<String, Arc<VarRef>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct VarRef {
|
||||
pub export: bool,
|
||||
pub var_type: Arc<TypeRef>,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct FunctionRef {
|
||||
pub export: bool,
|
||||
pub name: String,
|
||||
pub params: HashMap<String, Arc<VarRef>>,
|
||||
pub return_type: Option<Arc<TypeRef>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StructRef {
|
||||
pub export: bool,
|
||||
pub name: String,
|
||||
pub fields: HashMap<String, Arc<VarRef>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TopLevelHirScope {
|
||||
pub variables: HashMap<String, Arc<VarRef>>,
|
||||
pub var_idx: AtomicUsize,
|
||||
}
|
||||
|
||||
impl TopLevelHirScope {
|
||||
pub fn from_tlss(tlss: &TopLevelSymbolScope) -> Self {
|
||||
let mut scope = Self {
|
||||
variables: HashMap::new(),
|
||||
var_idx: AtomicUsize::new(0),
|
||||
};
|
||||
|
||||
for (_, symbol) in &tlss.functions {
|
||||
scope.function_ref(tlss, symbol);
|
||||
}
|
||||
|
||||
for (_, symbol) in &tlss.structs {
|
||||
scope.struct_ref(tlss, symbol);
|
||||
}
|
||||
|
||||
for (_, symbol) in &tlss.imports {
|
||||
scope.variables.insert(
|
||||
symbol.package_name.clone(),
|
||||
Arc::new(VarRef {
|
||||
export: false,
|
||||
name: symbol.package_name.clone(),
|
||||
var_type: Arc::new(TypeRef::Package(PackageRef::from_tlss(symbol))),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
scope
|
||||
}
|
||||
|
||||
pub fn function_ref(&mut self, tlss: &TopLevelSymbolScope, symbol: &FunctionSymbol) {
|
||||
if self.variables.get(&symbol.name).is_none() {
|
||||
let name = self.get_name(symbol.export);
|
||||
|
||||
if let Some(_) = tlss.functions.get(&symbol.name) {
|
||||
let rf = FunctionRef {
|
||||
export: symbol.export,
|
||||
name: name.clone(),
|
||||
params: symbol
|
||||
.params
|
||||
.iter()
|
||||
.map(|(name, v)| (name.clone(), self.var_ref(tlss, v)))
|
||||
.collect(),
|
||||
return_type: symbol
|
||||
.return_type
|
||||
.clone()
|
||||
.map(|rt| self.type_ref(tlss, &rt)),
|
||||
};
|
||||
|
||||
self.variables.insert(
|
||||
symbol.name.clone(),
|
||||
Arc::new(VarRef {
|
||||
export: false,
|
||||
name: name.clone(),
|
||||
var_type: Arc::new(TypeRef::Function(rf)),
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn struct_ref(
|
||||
&mut self,
|
||||
tlss: &TopLevelSymbolScope,
|
||||
symbol: &StructSymbol,
|
||||
) -> Arc<TypeRef> {
|
||||
if let Some(rf) = self.variables.get(&symbol.name) {
|
||||
rf.var_type.clone()
|
||||
} else {
|
||||
let name = self.get_name(symbol.export);
|
||||
|
||||
let rf = Arc::new(TypeRef::Struct(StructRef {
|
||||
export: symbol.export,
|
||||
name: name.clone(),
|
||||
fields: symbol
|
||||
.fields
|
||||
.iter()
|
||||
.map(|(name, v)| (name.clone(), self.var_ref(tlss, v)))
|
||||
.collect(),
|
||||
}));
|
||||
|
||||
self.variables.insert(
|
||||
symbol.name.clone(),
|
||||
Arc::new(VarRef {
|
||||
export: symbol.export,
|
||||
name: name.clone(),
|
||||
var_type: rf.clone(),
|
||||
}),
|
||||
);
|
||||
|
||||
rf
|
||||
}
|
||||
}
|
||||
|
||||
pub fn var_ref(&mut self, tlss: &TopLevelSymbolScope, symbol: &VarSymbol) -> Arc<VarRef> {
|
||||
Arc::new(VarRef {
|
||||
export: symbol.export,
|
||||
var_type: self.type_ref(tlss, &symbol.var_type),
|
||||
name: self.get_name(symbol.export),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn type_ref(&mut self, tlss: &TopLevelSymbolScope, symbol: &TypeSymbol) -> Arc<TypeRef> {
|
||||
if let Some(var_ref) = self.variables.get(&symbol.0) {
|
||||
var_ref.var_type.clone()
|
||||
} else {
|
||||
if let Some(tlss_rf) = tlss.structs.get(&symbol.0) {
|
||||
self.struct_ref(tlss, tlss_rf)
|
||||
} else {
|
||||
let var_ref = Arc::new(VarRef {
|
||||
export: false,
|
||||
name: symbol.0.clone(),
|
||||
var_type: Arc::new(TypeRef::Name(symbol.0.clone())),
|
||||
});
|
||||
|
||||
self.variables.insert(symbol.0.clone(), var_ref.clone());
|
||||
|
||||
var_ref.var_type.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_reference(&self, name: &String) -> Option<Arc<VarRef>> {
|
||||
self.variables.get(name).cloned()
|
||||
}
|
||||
|
||||
pub fn next_var_idx(&self) -> usize {
|
||||
self.var_idx.fetch_add(1, Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub fn get_name(&self, export: bool) -> String {
|
||||
format!("{}{}", if export { 'V' } else { 'v' }, self.next_var_idx())
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeRef {
|
||||
pub fn get_name(&self) -> String {
|
||||
match self {
|
||||
TypeRef::Function(f) => f.name.clone(),
|
||||
TypeRef::Struct(s) => s.name.clone(),
|
||||
TypeRef::Package(p) => p.name.clone(),
|
||||
TypeRef::Name(n) => n.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PackageRef {
|
||||
pub fn from_tlss(json_scope: &JSONScope) -> Self {
|
||||
let mut scope = Self {
|
||||
name: json_scope.package_name.clone(),
|
||||
variables: HashMap::new(),
|
||||
};
|
||||
|
||||
for (token_name, symbol) in &json_scope.functions {
|
||||
scope.function_ref(json_scope, token_name, symbol);
|
||||
}
|
||||
|
||||
for (_, symbol) in &json_scope.structs {
|
||||
scope.struct_ref(json_scope, symbol);
|
||||
}
|
||||
|
||||
scope
|
||||
}
|
||||
|
||||
pub fn function_ref(
|
||||
&mut self,
|
||||
json_scope: &JSONScope,
|
||||
token_name: &String,
|
||||
symbol: &FunctionSymbol,
|
||||
) {
|
||||
if self.variables.get(&symbol.name).is_none() {
|
||||
let name = symbol.name.clone();
|
||||
|
||||
if let Some(_) = json_scope.functions.get(token_name) {
|
||||
let rf = FunctionRef {
|
||||
export: symbol.export,
|
||||
name: name.clone(),
|
||||
params: symbol
|
||||
.params
|
||||
.iter()
|
||||
.map(|(name, v)| (name.clone(), self.var_ref(json_scope, v)))
|
||||
.collect(),
|
||||
return_type: symbol
|
||||
.return_type
|
||||
.clone()
|
||||
.map(|rt| self.type_ref(json_scope, &rt)),
|
||||
};
|
||||
|
||||
self.variables.insert(
|
||||
token_name.clone(),
|
||||
Arc::new(VarRef {
|
||||
export: false,
|
||||
name: name.clone(),
|
||||
var_type: Arc::new(TypeRef::Function(rf)),
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn struct_ref(&mut self, json_scope: &JSONScope, symbol: &StructSymbol) -> Arc<TypeRef> {
|
||||
if let Some(rf) = self.variables.get(&symbol.name) {
|
||||
rf.var_type.clone()
|
||||
} else {
|
||||
let name = symbol.name.clone();
|
||||
|
||||
let rf = Arc::new(TypeRef::Struct(StructRef {
|
||||
export: symbol.export,
|
||||
name: name.clone(),
|
||||
fields: symbol
|
||||
.fields
|
||||
.iter()
|
||||
.map(|(name, v)| (name.clone(), self.var_ref(json_scope, v)))
|
||||
.collect(),
|
||||
}));
|
||||
|
||||
self.variables.insert(
|
||||
symbol.name.clone(),
|
||||
Arc::new(VarRef {
|
||||
export: symbol.export,
|
||||
name: name.clone(),
|
||||
var_type: rf.clone(),
|
||||
}),
|
||||
);
|
||||
|
||||
rf
|
||||
}
|
||||
}
|
||||
|
||||
pub fn var_ref(&mut self, json_scope: &JSONScope, symbol: &VarSymbol) -> Arc<VarRef> {
|
||||
Arc::new(VarRef {
|
||||
export: symbol.export,
|
||||
var_type: self.type_ref(json_scope, &symbol.var_type),
|
||||
name: symbol.name.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn type_ref(&mut self, json_scope: &JSONScope, symbol: &TypeSymbol) -> Arc<TypeRef> {
|
||||
if let Some(var_ref) = self.variables.get(&symbol.0) {
|
||||
var_ref.var_type.clone()
|
||||
} else {
|
||||
if let Some(json_scope_rf) = json_scope.structs.get(&symbol.0) {
|
||||
self.struct_ref(json_scope, json_scope_rf)
|
||||
} else {
|
||||
let var_ref = Arc::new(VarRef {
|
||||
export: false,
|
||||
name: symbol.0.clone(),
|
||||
var_type: Arc::new(TypeRef::Name(symbol.0.clone())),
|
||||
});
|
||||
|
||||
self.variables.insert(symbol.0.clone(), var_ref.clone());
|
||||
|
||||
var_ref.var_type.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_reference(&self, name: &String) -> Option<Arc<VarRef>> {
|
||||
self.variables.get(name).cloned()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use parser::ast::{ParamList, TypeExpr};
|
||||
|
||||
use crate::{
|
||||
hir::VarRef,
|
||||
scope::{LocalScope, Scope},
|
||||
};
|
||||
|
||||
pub mod hir;
|
||||
pub mod scope;
|
||||
pub mod top_level;
|
||||
|
||||
pub fn walk_ast(top_scope: Arc<Scope>, tl: &mut Vec<parser::ast::TopLevel>) {
|
||||
for tl in tl {
|
||||
match tl {
|
||||
parser::ast::TopLevel::Import(_) => {}
|
||||
parser::ast::TopLevel::Package(_) => {}
|
||||
|
||||
parser::ast::TopLevel::FunctionDecl {
|
||||
params, body, name, ..
|
||||
} => {
|
||||
let rf = top_scope.get_reference(name).unwrap();
|
||||
|
||||
if name != "main" {
|
||||
*name = rf.name.clone();
|
||||
}
|
||||
|
||||
let scope = LocalScope::new(top_scope.clone());
|
||||
|
||||
scope.with_params(params);
|
||||
|
||||
scope.with_block(body);
|
||||
}
|
||||
|
||||
parser::ast::TopLevel::StructDecl { name, fields, .. } => {
|
||||
let rf = top_scope.get_reference(name).unwrap();
|
||||
*name = rf.name.clone();
|
||||
|
||||
match &*rf.var_type {
|
||||
hir::TypeRef::Struct(s) => walk_param_list(&s.fields, fields),
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn walk_param_list(fields: &HashMap<String, Arc<VarRef>>, param_list: &mut ParamList) {
|
||||
let old_param_list = param_list.clone();
|
||||
param_list.0.clear();
|
||||
|
||||
for (name, param) in fields {
|
||||
param_list.0.insert(
|
||||
param.name.clone(),
|
||||
(
|
||||
old_param_list.0.get(name).map(|a| a.0).unwrap_or_default(),
|
||||
TypeExpr::Identifier(param.var_type.get_name()),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use parser::ast::{self, ParamList, Postfix, Statement};
|
||||
|
||||
use crate::{
|
||||
hir::{TopLevelHirScope, TypeRef, VarRef},
|
||||
top_level::TopLevelSymbolScope,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Scope {
|
||||
TopLevel(TopLevelHirScope),
|
||||
Local(LocalScope),
|
||||
}
|
||||
|
||||
impl Scope {
|
||||
pub fn from_top(
|
||||
path: &std::path::PathBuf,
|
||||
top_level: &Vec<parser::ast::TopLevel>,
|
||||
) -> Arc<Self> {
|
||||
let tl = TopLevelSymbolScope::from(path, top_level);
|
||||
Arc::new(Self::TopLevel(TopLevelHirScope::from_tlss(&tl)))
|
||||
}
|
||||
|
||||
pub fn get_reference(&self, name: &String) -> Option<Arc<VarRef>> {
|
||||
match self {
|
||||
Scope::TopLevel(tl) => tl.get_reference(name),
|
||||
Scope::Local(l) => l.get_reference(name),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_var_idx(&self) -> usize {
|
||||
match self {
|
||||
Scope::TopLevel(tl) => tl.next_var_idx(),
|
||||
Scope::Local(l) => l.parent.next_var_idx(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_name(&self, export: bool) -> String {
|
||||
format!("{}{}", if export { 'V' } else { 'v' }, self.next_var_idx())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LocalScope {
|
||||
pub parent: Arc<Scope>,
|
||||
pub variables: Mutex<HashMap<String, Arc<VarRef>>>,
|
||||
}
|
||||
|
||||
impl LocalScope {
|
||||
pub fn new(parent: Arc<Scope>) -> Arc<Self> {
|
||||
Arc::new(LocalScope {
|
||||
parent,
|
||||
variables: Mutex::new(HashMap::new()),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_reference(&self, name: &String) -> Option<Arc<VarRef>> {
|
||||
self.variables
|
||||
.lock()
|
||||
.unwrap()
|
||||
.get(name)
|
||||
.cloned()
|
||||
.or_else(|| self.parent.get_reference(name))
|
||||
}
|
||||
|
||||
pub fn get_type_reference(&self, name: &String) -> Option<Arc<VarRef>> {
|
||||
match self.get_reference(name) {
|
||||
Some(v) => Some(v),
|
||||
None => {
|
||||
let var_ref = Arc::new(VarRef {
|
||||
export: false,
|
||||
name: name.clone(),
|
||||
var_type: Arc::new(TypeRef::Name(name.clone())),
|
||||
});
|
||||
|
||||
self.variables
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(name.clone(), var_ref.clone());
|
||||
|
||||
Some(var_ref)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_statement(self: &Arc<Self>, statement: &mut Statement) {
|
||||
match statement {
|
||||
Statement::Block(b) => self.clone().with_block(b),
|
||||
Statement::VarDecl {
|
||||
name, init, type_, ..
|
||||
} => {
|
||||
if let Some(init) = init {
|
||||
let init_type = self.get_type_from_expr(init);
|
||||
let var_type = type_
|
||||
.as_mut()
|
||||
.map(|t| self.get_type_from_type_expr(t))
|
||||
.or_else(|| Some(init_type))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
let var_name = name.clone();
|
||||
|
||||
*name = self.parent.get_name(false);
|
||||
|
||||
self.variables.lock().unwrap().insert(
|
||||
var_name,
|
||||
Arc::new(VarRef {
|
||||
export: false,
|
||||
name: name.clone(),
|
||||
var_type,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
Statement::Expression(e) => {
|
||||
self.get_type_from_expr(e);
|
||||
}
|
||||
Statement::If {
|
||||
condition,
|
||||
then_branch,
|
||||
else_branch,
|
||||
} => {
|
||||
self.get_type_from_expr(condition);
|
||||
self.clone().with_statement(then_branch);
|
||||
if let Some(else_branch) = else_branch {
|
||||
self.clone().with_statement(else_branch);
|
||||
}
|
||||
}
|
||||
Statement::Return(expr) => {
|
||||
if let Some(expr) = expr {
|
||||
self.get_type_from_expr(expr);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_block(self: &Arc<Self>, block: &mut parser::ast::Block) {
|
||||
for statement in &mut block.0 {
|
||||
self.with_statement(statement);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn walk_postfixes(
|
||||
self: &Arc<Self>,
|
||||
initial: &mut Box<ast::Expression>,
|
||||
postfixes: &mut Vec<Postfix>,
|
||||
) -> Option<Arc<TypeRef>> {
|
||||
let mut current_type = self.get_type_from_expr(initial)?;
|
||||
|
||||
for postfix in postfixes {
|
||||
match postfix {
|
||||
Postfix::FieldAccess(id) => match &*current_type {
|
||||
TypeRef::Struct(s) => {
|
||||
let field = s.fields.get(id)?;
|
||||
*id = field.name.clone();
|
||||
current_type = field.var_type.clone();
|
||||
}
|
||||
TypeRef::Package(p) => {
|
||||
let var_ref = p.variables.get(id)?;
|
||||
*id = var_ref.name.clone();
|
||||
current_type = var_ref.var_type.clone();
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
},
|
||||
Postfix::Call(args) => match &*current_type {
|
||||
TypeRef::Function(s) => {
|
||||
for arg in args {
|
||||
self.get_type_from_expr(arg);
|
||||
}
|
||||
current_type = s.return_type.clone()?;
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
},
|
||||
Postfix::StructCall(fields) => match &*current_type {
|
||||
TypeRef::Struct(s) => {
|
||||
let mut old_fields = fields.clone();
|
||||
|
||||
fields.clear();
|
||||
|
||||
for (name, mut expr) in old_fields.drain() {
|
||||
self.get_type_from_expr(&mut expr);
|
||||
fields.insert(s.fields.get(&name).unwrap().name.clone(), expr);
|
||||
}
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
},
|
||||
Postfix::Binary(op, right) => match op {
|
||||
parser::ast::BinaryOp::Equal
|
||||
| parser::ast::BinaryOp::NotEqual
|
||||
| parser::ast::BinaryOp::GreaterThan
|
||||
| parser::ast::BinaryOp::LessThan
|
||||
| parser::ast::BinaryOp::GreaterThanOrEqual
|
||||
| parser::ast::BinaryOp::LessThanOrEqual => {
|
||||
self.get_type_from_expr(right)?;
|
||||
current_type = Arc::new(TypeRef::Name("bool".to_string()));
|
||||
}
|
||||
parser::ast::BinaryOp::Plus
|
||||
| parser::ast::BinaryOp::Minus
|
||||
| parser::ast::BinaryOp::Multiply
|
||||
| parser::ast::BinaryOp::Divide
|
||||
| parser::ast::BinaryOp::Modulo => {
|
||||
self.get_type_from_expr(right)?;
|
||||
}
|
||||
},
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
Some(current_type)
|
||||
}
|
||||
|
||||
pub fn get_type_from_type_expr(
|
||||
self: &Arc<Self>,
|
||||
expr: &mut ast::TypeExpr,
|
||||
) -> Option<Arc<TypeRef>> {
|
||||
match expr {
|
||||
ast::TypeExpr::Identifier(id) => {
|
||||
if let Some(v) = self.get_type_reference(id).map(|r| r.var_type.clone()) {
|
||||
*id = v.get_name();
|
||||
Some(v)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_type_from_expr(
|
||||
self: &Arc<Self>,
|
||||
expr: &mut ast::Expression,
|
||||
) -> Option<Arc<TypeRef>> {
|
||||
match expr {
|
||||
ast::Expression::IntLiteral(_) => self
|
||||
.get_type_reference(&"int".to_string())
|
||||
.map(|r| r.var_type.clone()),
|
||||
|
||||
ast::Expression::FloatLiteral(_) => self
|
||||
.get_type_reference(&"float".to_string())
|
||||
.map(|r| r.var_type.clone()),
|
||||
|
||||
ast::Expression::BoolLiteral(_) => self
|
||||
.get_type_reference(&"bool".to_string())
|
||||
.map(|r| r.var_type.clone()),
|
||||
|
||||
ast::Expression::StringLiteral(_) => self
|
||||
.get_type_reference(&"string".to_string())
|
||||
.map(|r| r.var_type.clone()),
|
||||
|
||||
ast::Expression::Identifier(id) => {
|
||||
let rf = self.get_reference(id)?;
|
||||
*id = rf.name.clone();
|
||||
Some(rf.var_type.clone())
|
||||
}
|
||||
|
||||
ast::Expression::Postfix { initial, postfixes } => {
|
||||
self.walk_postfixes(initial, postfixes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_params(self: &Arc<Self>, param_list: &mut ParamList) {
|
||||
let mut new_param_list = HashMap::new();
|
||||
|
||||
for (param_name, (export, type_expr)) in param_list.0.drain() {
|
||||
match &type_expr {
|
||||
parser::ast::TypeExpr::Identifier(id) => {
|
||||
let new_name = self.parent.get_name(export);
|
||||
|
||||
self.variables.lock().unwrap().insert(
|
||||
param_name.clone(),
|
||||
Arc::new(VarRef {
|
||||
export: export,
|
||||
var_type: self
|
||||
.parent
|
||||
.get_reference(id)
|
||||
.map(|r| r.var_type.clone())
|
||||
.unwrap(),
|
||||
name: new_name.clone(),
|
||||
}),
|
||||
);
|
||||
|
||||
new_param_list.insert(new_name, (export, type_expr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
param_list.0 = new_param_list;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use parser::ast::TopLevel;
|
||||
|
||||
use parser::ast::{ParamList, TypeExpr};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct TypeSymbol(pub String);
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct VarSymbol {
|
||||
pub export: bool,
|
||||
pub var_type: TypeSymbol,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct FunctionSymbol {
|
||||
pub export: bool,
|
||||
pub name: String,
|
||||
pub params: HashMap<String, VarSymbol>,
|
||||
pub return_type: Option<TypeSymbol>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct StructSymbol {
|
||||
pub export: bool,
|
||||
pub name: String,
|
||||
pub fields: HashMap<String, VarSymbol>,
|
||||
pub methods: HashMap<String, FunctionSymbol>,
|
||||
}
|
||||
|
||||
impl TypeSymbol {
|
||||
pub fn from_ast(expr: TypeExpr) -> Self {
|
||||
TypeSymbol(match expr {
|
||||
TypeExpr::Identifier(i) => i.to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl FunctionSymbol {
|
||||
pub fn from_ast(
|
||||
export: bool,
|
||||
name: String,
|
||||
params: ParamList,
|
||||
return_type: Option<TypeExpr>,
|
||||
) -> Self {
|
||||
Self {
|
||||
export: export,
|
||||
name: name.clone(),
|
||||
params: params
|
||||
.0
|
||||
.iter()
|
||||
.map(|(name, (export, v))| {
|
||||
(
|
||||
name.clone(),
|
||||
VarSymbol {
|
||||
export: *export,
|
||||
name: name.clone(),
|
||||
var_type: TypeSymbol(match v {
|
||||
TypeExpr::Identifier(i) => i.to_string(),
|
||||
}),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
return_type: return_type.map(TypeSymbol::from_ast),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StructSymbol {
|
||||
pub fn from_ast(export: bool, name: String, fields: ParamList) -> Self {
|
||||
Self {
|
||||
export,
|
||||
name,
|
||||
fields: fields
|
||||
.0
|
||||
.iter()
|
||||
.map(|(name, (export, v))| {
|
||||
(
|
||||
name.clone(),
|
||||
VarSymbol {
|
||||
export: *export,
|
||||
name: name.clone(),
|
||||
var_type: TypeSymbol(match v {
|
||||
TypeExpr::Identifier(i) => i.to_string(),
|
||||
}),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
// TODO - parse struct methods
|
||||
methods: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct JSONScope {
|
||||
pub structs: HashMap<String, StructSymbol>,
|
||||
pub functions: HashMap<String, FunctionSymbol>,
|
||||
pub package_name: String,
|
||||
}
|
||||
|
||||
type JsonScopeMap = HashMap<String, JSONScope>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TopLevelSymbolScope {
|
||||
pub imports: HashMap<String, JSONScope>,
|
||||
pub structs: HashMap<String, StructSymbol>,
|
||||
pub functions: HashMap<String, FunctionSymbol>,
|
||||
}
|
||||
|
||||
impl TopLevelSymbolScope {
|
||||
pub fn from(path: &PathBuf, top_level: &Vec<TopLevel>) -> Self {
|
||||
let mut scope = TopLevelSymbolScope {
|
||||
imports: HashMap::new(),
|
||||
structs: HashMap::new(),
|
||||
functions: HashMap::new(),
|
||||
};
|
||||
|
||||
for top in top_level {
|
||||
match top {
|
||||
TopLevel::Import(package) => {
|
||||
let json = fs::read(path.join("mist.map.json")).unwrap();
|
||||
let json_scope_map: JsonScopeMap = serde_json::from_slice(&json).unwrap();
|
||||
let json_scope = json_scope_map
|
||||
.get(&package.replace("\"", ""))
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
scope
|
||||
.imports
|
||||
.insert(json_scope.package_name.clone(), json_scope);
|
||||
}
|
||||
TopLevel::Package(_) => {}
|
||||
|
||||
TopLevel::FunctionDecl {
|
||||
export,
|
||||
name,
|
||||
params,
|
||||
return_type,
|
||||
..
|
||||
} => {
|
||||
scope.functions.insert(
|
||||
name.clone(),
|
||||
FunctionSymbol::from_ast(
|
||||
*export,
|
||||
name.clone(),
|
||||
params.clone(),
|
||||
return_type.clone(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
TopLevel::StructDecl {
|
||||
export,
|
||||
name,
|
||||
fields,
|
||||
} => {
|
||||
scope.structs.insert(
|
||||
name.clone(),
|
||||
StructSymbol::from_ast(*export, name.clone(), fields.clone()),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scope
|
||||
}
|
||||
}
|
||||
+379
@@ -0,0 +1,379 @@
|
||||
use parser::ast::{BinaryOp, Block, Expression, Postfix, Statement, TopLevel, TypeExpr, VarKind};
|
||||
|
||||
pub struct GoCodegen {
|
||||
output: String,
|
||||
indent: usize,
|
||||
}
|
||||
|
||||
impl GoCodegen {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
output: String::new(),
|
||||
indent: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn indent_str(&self) -> String {
|
||||
" ".repeat(self.indent)
|
||||
}
|
||||
|
||||
fn add_indented(&mut self, s: &str) {
|
||||
self.output.push_str(&format!("{}{}", self.indent_str(), s));
|
||||
}
|
||||
|
||||
fn add(&mut self, s: &str) {
|
||||
self.output.push_str(s);
|
||||
}
|
||||
|
||||
fn addln(&mut self, s: &str) {
|
||||
self.add(s);
|
||||
self.add("\n");
|
||||
}
|
||||
|
||||
fn add_indentedln(&mut self, s: &str) {
|
||||
self.add_indented(s);
|
||||
self.add("\n");
|
||||
}
|
||||
|
||||
pub fn generate(&mut self, toplevels: &[TopLevel]) -> String {
|
||||
for tl in toplevels {
|
||||
self.generate_toplevel(tl);
|
||||
}
|
||||
self.output.clone()
|
||||
}
|
||||
|
||||
fn generate_toplevel(&mut self, tl: &TopLevel) {
|
||||
match tl {
|
||||
TopLevel::Import(path) => {
|
||||
let import_path = path.replace("\"", "");
|
||||
if import_path.starts_with("./") || import_path.starts_with("/") {
|
||||
self.addln(&format!("import \"{}\"", import_path));
|
||||
} else {
|
||||
self.addln(&format!("import \"{}\"", import_path));
|
||||
}
|
||||
self.addln("");
|
||||
}
|
||||
TopLevel::StructDecl {
|
||||
export,
|
||||
name,
|
||||
fields,
|
||||
} => {
|
||||
let name = if *export { name } else { name };
|
||||
self.addln(&format!("type {} struct {{", name));
|
||||
self.indent += 1;
|
||||
for (field_name, (_, ty)) in &fields.0 {
|
||||
let go_ty = self.translate_type(ty);
|
||||
self.addln(&format!("{} {}", field_name, go_ty));
|
||||
}
|
||||
self.indent -= 1;
|
||||
self.addln("}\n");
|
||||
}
|
||||
TopLevel::FunctionDecl {
|
||||
export,
|
||||
name,
|
||||
params,
|
||||
return_type,
|
||||
body,
|
||||
} => {
|
||||
let name = if *export {
|
||||
format!("{}", name)
|
||||
} else {
|
||||
name.clone()
|
||||
};
|
||||
let params_str = params
|
||||
.0
|
||||
.iter()
|
||||
.map(|(n, (_, t))| format!("{} {}", n, self.translate_type(t)))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
|
||||
let ret_ty = return_type
|
||||
.as_ref()
|
||||
.map(|t| self.translate_type(t))
|
||||
.unwrap_or_else(|| "".to_string());
|
||||
|
||||
if ret_ty.is_empty() {
|
||||
self.addln(&format!("func {}({}) {{", name, params_str));
|
||||
} else {
|
||||
self.addln(&format!("func {}({}) {} {{", name, params_str, ret_ty));
|
||||
}
|
||||
self.indent += 1;
|
||||
self.generate_block(body);
|
||||
self.indent -= 1;
|
||||
self.addln("}\n");
|
||||
}
|
||||
TopLevel::Package(pkg) => {
|
||||
self.addln(&format!("package {}", pkg));
|
||||
self.addln("");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn translate_type(&self, ty: &TypeExpr) -> String {
|
||||
match ty {
|
||||
TypeExpr::Identifier(name) => match name.as_str() {
|
||||
"int" => "int".to_string(),
|
||||
"float" | "float64" => "float64".to_string(),
|
||||
"float32" => "float32".to_string(),
|
||||
"bool" => "bool".to_string(),
|
||||
"string" => "string".to_string(),
|
||||
"byte" => "byte".to_string(),
|
||||
"rune" => "rune".to_string(),
|
||||
_ => name.clone(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_block(&mut self, block: &Block) {
|
||||
for stmt in &block.0 {
|
||||
self.generate_statement(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_statement(&mut self, stmt: &Statement) {
|
||||
match stmt {
|
||||
Statement::Expression(expr) => {
|
||||
self.add_indentedln(&format!("{};", self.generate_expression(expr)));
|
||||
}
|
||||
Statement::Block(block) => {
|
||||
self.add_indentedln("{");
|
||||
self.indent += 1;
|
||||
self.generate_block(block);
|
||||
self.indent -= 1;
|
||||
self.add_indentedln("}\n");
|
||||
}
|
||||
Statement::VarDecl {
|
||||
kind,
|
||||
name,
|
||||
init,
|
||||
type_,
|
||||
} => {
|
||||
let go_kind = match kind {
|
||||
VarKind::Let | VarKind::Const => "var",
|
||||
VarKind::Var => "var",
|
||||
};
|
||||
let init_expr = init
|
||||
.as_ref()
|
||||
.map(|e| format!(" = {}", self.generate_expression(e)))
|
||||
.unwrap_or_else(|| "".to_string());
|
||||
let type_expr = type_
|
||||
.clone()
|
||||
.map(|t| self.translate_type(&t))
|
||||
.unwrap_or_default();
|
||||
self.add_indentedln(&format!(
|
||||
"{} {} {}{};\n",
|
||||
go_kind, name, type_expr, init_expr
|
||||
));
|
||||
}
|
||||
Statement::VarAssign { target, value } => {
|
||||
self.add_indentedln(&format!(
|
||||
"{} = {};\n",
|
||||
self.generate_expression(target),
|
||||
self.generate_expression(value)
|
||||
));
|
||||
}
|
||||
Statement::If {
|
||||
condition,
|
||||
then_branch,
|
||||
else_branch,
|
||||
} => {
|
||||
self.add_indented(&format!("if {} ", self.generate_expression(condition)));
|
||||
self.generate_statement(then_branch);
|
||||
if let Some(else_br) = else_branch {
|
||||
self.add_indented("else ");
|
||||
self.generate_statement(else_br);
|
||||
}
|
||||
}
|
||||
Statement::While { condition, body } => {
|
||||
self.add_indented(&format!("for {} ", self.generate_expression(condition)));
|
||||
self.generate_statement(body);
|
||||
}
|
||||
Statement::For {
|
||||
init,
|
||||
condition,
|
||||
update,
|
||||
body,
|
||||
} => {
|
||||
let (kind, init_name, init_val) = init;
|
||||
let init_expr = init_val
|
||||
.as_ref()
|
||||
.map(|e| format!(" = {}", self.generate_expression(e)))
|
||||
.unwrap_or_else(|| "".to_string());
|
||||
let init_str = format!("{} {}{}", self.var_kind_to_go(kind), init_name, init_expr);
|
||||
|
||||
let cond_str = condition
|
||||
.as_ref()
|
||||
.map(|e| self.generate_expression(e))
|
||||
.unwrap_or_else(|| "true".to_string());
|
||||
|
||||
let update_str = update
|
||||
.as_ref()
|
||||
.map(|s| self.generate_expression(&self.stmt_to_expr(s)))
|
||||
.unwrap_or_else(|| "".to_string());
|
||||
|
||||
self.add_indented(&format!("for {}; {}; {} ", init_str, cond_str, update_str));
|
||||
self.generate_statement(body);
|
||||
}
|
||||
Statement::Return(expr) => {
|
||||
let ret_val = expr
|
||||
.as_ref()
|
||||
.map(|e| self.generate_expression(e))
|
||||
.unwrap_or_else(|| "".to_string());
|
||||
self.add_indentedln(&format!("return {};\n", ret_val));
|
||||
}
|
||||
Statement::Break => {
|
||||
self.add_indentedln("break;\n");
|
||||
}
|
||||
Statement::Continue => {
|
||||
self.add_indentedln("continue;\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn stmt_to_expr(&self, stmt: &Statement) -> Expression {
|
||||
match stmt {
|
||||
Statement::Expression(e) => e.clone(),
|
||||
_ => Expression::Identifier(String::new()),
|
||||
}
|
||||
}
|
||||
|
||||
fn var_kind_to_go(&self, kind: &VarKind) -> String {
|
||||
match kind {
|
||||
VarKind::Let | VarKind::Const => "var".to_string(),
|
||||
VarKind::Var => "var".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_expression(&self, expr: &Expression) -> String {
|
||||
match expr {
|
||||
Expression::Identifier(name) => name.clone(),
|
||||
Expression::IntLiteral(n) => n.to_string(),
|
||||
Expression::FloatLiteral(n) => n.to_string(),
|
||||
Expression::BoolLiteral(b) => b.to_string(),
|
||||
Expression::StringLiteral(s) => format!("\"{}\"", s),
|
||||
Expression::Postfix { initial, postfixes } => {
|
||||
let base = self.generate_expression(initial);
|
||||
self.apply_postfixes(&base, postfixes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_postfixes(&self, base: &str, postfixes: &[Postfix]) -> String {
|
||||
let mut result = base.to_string();
|
||||
for postfix in postfixes {
|
||||
result = match postfix {
|
||||
Postfix::FieldAccess(field) => format!("{}.{}", result, field),
|
||||
Postfix::Call(args) => {
|
||||
let args_str = args
|
||||
.iter()
|
||||
.map(|a| self.generate_expression(a))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
format!("{}({})", result, args_str)
|
||||
}
|
||||
Postfix::StructCall(fields) => {
|
||||
let args_str = fields
|
||||
.iter()
|
||||
.map(|(a, b)| format!("{a}: {}", self.generate_expression(b)))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
format!("{}{{{}}}", result, args_str)
|
||||
}
|
||||
Postfix::Index(idx) => format!("{}[{}]", result, self.generate_expression(idx)),
|
||||
Postfix::Binary(op, rhs) => {
|
||||
let op_str = match op {
|
||||
BinaryOp::Plus => "+",
|
||||
BinaryOp::Minus => "-",
|
||||
BinaryOp::Multiply => "*",
|
||||
BinaryOp::Divide => "/",
|
||||
BinaryOp::Modulo => "%",
|
||||
BinaryOp::Equal => "==",
|
||||
BinaryOp::NotEqual => "!=",
|
||||
BinaryOp::LessThan => "<",
|
||||
BinaryOp::GreaterThan => ">",
|
||||
BinaryOp::LessThanOrEqual => "<=",
|
||||
BinaryOp::GreaterThanOrEqual => ">=",
|
||||
};
|
||||
format!("{} {} {}", result, op_str, self.generate_expression(rhs))
|
||||
}
|
||||
};
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for GoCodegen {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use parser::ast::{Block, Expression, ParamList, Statement, TopLevel, TypeExpr};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn test_int_literal() {
|
||||
let cg = GoCodegen::new();
|
||||
let expr = Expression::IntLiteral(42);
|
||||
let result = cg.generate_expression(&expr);
|
||||
assert_eq!(result, "42");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_string_literal() {
|
||||
let cg = GoCodegen::new();
|
||||
let expr = Expression::StringLiteral("hello".to_string());
|
||||
let result = cg.generate_expression(&expr);
|
||||
assert_eq!(result, "\"hello\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_function_decl() {
|
||||
let mut cg = GoCodegen::new();
|
||||
let toplevel = TopLevel::FunctionDecl {
|
||||
export: true,
|
||||
name: "main".to_string(),
|
||||
params: ParamList(HashMap::new()),
|
||||
return_type: None,
|
||||
body: Block(vec![Statement::Return(None)]),
|
||||
};
|
||||
cg.generate_toplevel(&toplevel);
|
||||
let output = cg.output.clone();
|
||||
assert!(output.contains("func main()"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_struct_decl() {
|
||||
let mut cg = GoCodegen::new();
|
||||
let mut fields = HashMap::new();
|
||||
fields.insert(
|
||||
"x".to_string(),
|
||||
(true, TypeExpr::Identifier("int".to_string())),
|
||||
);
|
||||
let toplevel = TopLevel::StructDecl {
|
||||
export: true,
|
||||
name: "Point".to_string(),
|
||||
fields: ParamList(fields),
|
||||
};
|
||||
cg.generate_toplevel(&toplevel);
|
||||
let output = cg.output.clone();
|
||||
assert!(output.contains("type Point struct"));
|
||||
assert!(output.contains("x int"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_if_statement() {
|
||||
let mut cg = GoCodegen::new();
|
||||
let stmt = Statement::If {
|
||||
condition: Expression::Identifier("x".to_string()),
|
||||
then_branch: Box::new(Statement::Return(Some(Expression::IntLiteral(1)))),
|
||||
else_branch: None,
|
||||
};
|
||||
cg.generate_statement(&stmt);
|
||||
let output = cg.output.clone();
|
||||
assert!(output.contains("if x"));
|
||||
}
|
||||
}
|
||||
+31
-19
@@ -1,13 +1,11 @@
|
||||
use std::{fs, path::PathBuf, process, time::Instant};
|
||||
|
||||
use crate::{gogen, parser};
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Config {
|
||||
entry: String,
|
||||
out_dir: String,
|
||||
src: String,
|
||||
output: String,
|
||||
}
|
||||
|
||||
pub fn build() {
|
||||
@@ -23,10 +21,22 @@ pub fn build() {
|
||||
// 2. load config
|
||||
let config = load_config(&root);
|
||||
|
||||
let entry_path = root.join(&config.entry);
|
||||
let out_dir = root.join(&config.out_dir);
|
||||
let src = root.join(&config.src);
|
||||
|
||||
println!(" → entry: {}", entry_path.display());
|
||||
for entry in fs::read_dir(src).unwrap() {
|
||||
if let Ok(entry) = entry {
|
||||
let entry_path = root.join(&entry.path());
|
||||
let out_dir = root.join(&config.output);
|
||||
let file_name = entry.file_name();
|
||||
let file_name = file_name.to_string_lossy();
|
||||
|
||||
let script = if file_name.ends_with(".ms") {
|
||||
true
|
||||
} else if file_name.ends_with(".mist") {
|
||||
false
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// 3. read entry file
|
||||
let source = match fs::read_to_string(&entry_path) {
|
||||
@@ -37,40 +47,42 @@ pub fn build() {
|
||||
}
|
||||
};
|
||||
|
||||
println!(" → parsing...");
|
||||
let parser_result = if script {
|
||||
parser::script_parser::parse(&source).map_err(|e| e.to_string())
|
||||
} else {
|
||||
parser::parse(&source).map_err(|e| e.to_string())
|
||||
};
|
||||
|
||||
let ast = match parser::parse(&source) {
|
||||
Ok(ast) => {
|
||||
println!(" ✓ parsed {} items", ast.len());
|
||||
ast
|
||||
}
|
||||
let mut ast = match parser_result {
|
||||
Ok(ast) => ast,
|
||||
Err(e) => {
|
||||
eprintln!("error: parse failed\n{}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
println!(" → generating Go code...");
|
||||
semantic::walk_ast(semantic::scope::Scope::from_top(&root, &ast), &mut ast);
|
||||
|
||||
// let output = gogen::generate(&ast);
|
||||
let output = String::from("// TODO: implement code generation\n");
|
||||
let mut gc = crate::codegen::GoCodegen::new();
|
||||
let output = gc.generate(&ast);
|
||||
|
||||
// 4. ensure build dir
|
||||
if let Err(e) = fs::create_dir_all(&out_dir) {
|
||||
eprintln!("error: failed to create build dir\n {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
let out_file = out_dir.join("main.go");
|
||||
let out_file =
|
||||
out_dir.join(file_name.replace(if script { ".ms" } else { ".mist" }, ".go"));
|
||||
|
||||
if let Err(e) = fs::write(&out_file, output) {
|
||||
eprintln!("error: failed to write output\n {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let elapsed = start.elapsed();
|
||||
|
||||
println!(" ✓ built {}", out_file.display());
|
||||
println!("build finished in {:.2?}", elapsed);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,283 +0,0 @@
|
||||
// use crate::parser::ast::{
|
||||
// BinOperator, Class, Expression, Function, Statement, Struct, TopLevel, TopLevel, TypeExpr,
|
||||
// };
|
||||
|
||||
// pub fn generate(program: &TopLevel) -> String {
|
||||
// let mut out = String::new();
|
||||
|
||||
// out.push_str("package main\n\n");
|
||||
|
||||
// // imports
|
||||
// let mut imports = vec![];
|
||||
// for item in &program.statements {
|
||||
// if let TopLevel::Import(i) = item {
|
||||
// imports.push(i.path.clone());
|
||||
// }
|
||||
// }
|
||||
|
||||
// if !imports.is_empty() {
|
||||
// out.push_str("import (\n");
|
||||
// for imp in imports {
|
||||
// out.push_str(&format!(" {}\n", imp));
|
||||
// }
|
||||
// out.push_str(")\n\n");
|
||||
// }
|
||||
|
||||
// // rest
|
||||
// for item in &program.statements {
|
||||
// match item {
|
||||
// TopLevel::Function(f) => out.push_str(&gen_function(f)),
|
||||
// TopLevel::Struct(s) => out.push_str(&gen_struct(s)),
|
||||
// TopLevel::Class(c) => out.push_str(&gen_class(c)),
|
||||
// TopLevel::Import(_) => {}
|
||||
// }
|
||||
// out.push('\n');
|
||||
// }
|
||||
|
||||
// out
|
||||
// }
|
||||
|
||||
// fn gen_struct(s: &Struct) -> String {
|
||||
// let mut out = format!("type {} struct {{\n", s.name);
|
||||
|
||||
// for field in &s.fields {
|
||||
// out.push_str(&format!(
|
||||
// " {} {}\n",
|
||||
// capitalize(&field.name),
|
||||
// gen_type(&field.type_expr)
|
||||
// ));
|
||||
// }
|
||||
|
||||
// out.push_str("}\n");
|
||||
// out
|
||||
// }
|
||||
|
||||
// fn gen_class(c: &Class) -> String {
|
||||
// let mut out = String::new();
|
||||
|
||||
// // struct
|
||||
// out.push_str(&format!("type {} struct {{\n", c.name));
|
||||
// for field in &c.fields {
|
||||
// out.push_str(&format!(
|
||||
// " {} {}\n",
|
||||
// capitalize(&field.name),
|
||||
// gen_type(&field.type_expr)
|
||||
// ));
|
||||
// }
|
||||
// out.push_str("}\n\n");
|
||||
|
||||
// // methods
|
||||
// for method in &c.methods {
|
||||
// out.push_str(&gen_method(c, method));
|
||||
// out.push('\n');
|
||||
// }
|
||||
|
||||
// out
|
||||
// }
|
||||
|
||||
// fn gen_function(f: &Function) -> String {
|
||||
// let mut out = format!("func {}(", f.name);
|
||||
|
||||
// // params
|
||||
// for (i, p) in f.params.iter().enumerate() {
|
||||
// if i > 0 {
|
||||
// out.push_str(", ");
|
||||
// }
|
||||
// out.push_str(&format!("{} {}", p.name, gen_type(&p.type_expr)));
|
||||
// }
|
||||
|
||||
// out.push(')');
|
||||
|
||||
// // return
|
||||
// if let Some(ret) = &f.return_type {
|
||||
// out.push_str(&format!(" {}", gen_type(ret)));
|
||||
// }
|
||||
|
||||
// out.push_str(" {\n");
|
||||
|
||||
// for stmt in &f.body {
|
||||
// out.push_str(&gen_statement(stmt));
|
||||
// }
|
||||
|
||||
// out.push_str("}\n");
|
||||
// out
|
||||
// }
|
||||
|
||||
// fn gen_method(class: &Class, f: &Function) -> String {
|
||||
// let mut out = format!("func (self *{}) {}(", class.name, f.name);
|
||||
|
||||
// for (i, p) in f.params.iter().enumerate() {
|
||||
// if i > 0 {
|
||||
// out.push_str(", ");
|
||||
// }
|
||||
// out.push_str(&format!("{} {}", p.name, gen_type(&p.type_expr)));
|
||||
// }
|
||||
|
||||
// out.push(')');
|
||||
|
||||
// if let Some(ret) = &f.return_type {
|
||||
// out.push_str(&format!(" {}", gen_type(ret)));
|
||||
// }
|
||||
|
||||
// out.push_str(" {\n");
|
||||
|
||||
// for stmt in &f.body {
|
||||
// out.push_str(&gen_statement(stmt));
|
||||
// }
|
||||
|
||||
// out.push_str("}\n");
|
||||
|
||||
// out
|
||||
// }
|
||||
|
||||
// fn gen_statement(stmt: &Statement) -> String {
|
||||
// match stmt {
|
||||
// Statement::Let(s) => {
|
||||
// let mut out = format!(" {} := {}", s.name, gen_expr(&s.value));
|
||||
|
||||
// out.push_str(";\n");
|
||||
// out
|
||||
// }
|
||||
|
||||
// Statement::Return(r) => match &r.value {
|
||||
// Some(v) => format!(" return {};\n", gen_expr(v)),
|
||||
// None => " return;\n".to_string(),
|
||||
// },
|
||||
|
||||
// Statement::Expression(e) => {
|
||||
// format!(" {};\n", gen_expr(e))
|
||||
// }
|
||||
|
||||
// Statement::If(i) => {
|
||||
// let mut out = format!(" if {} {{\n", gen_expr(&i.condition));
|
||||
|
||||
// for stmt in &i.body {
|
||||
// out.push_str(&gen_statement(stmt));
|
||||
// }
|
||||
|
||||
// out.push_str(" }");
|
||||
|
||||
// if let Some(else_body) = &i.else_body {
|
||||
// out.push_str(" else {\n");
|
||||
// for stmt in else_body {
|
||||
// out.push_str(&gen_statement(stmt));
|
||||
// }
|
||||
// out.push_str(" }");
|
||||
// }
|
||||
|
||||
// out.push('\n');
|
||||
// out
|
||||
// }
|
||||
|
||||
// Statement::For(f) => {
|
||||
// let mut out = format!(
|
||||
// " for _, {} := range {} {{\n",
|
||||
// f.var,
|
||||
// gen_expr(&f.iterator)
|
||||
// );
|
||||
|
||||
// for stmt in &f.body {
|
||||
// out.push_str(&gen_statement(stmt));
|
||||
// }
|
||||
|
||||
// out.push_str(" }\n");
|
||||
// out
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// fn gen_expr(expr: &Expression) -> String {
|
||||
// match expr {
|
||||
// Expression::Identifier(name, _) => name.clone(),
|
||||
// Expression::Integer(v, _) => v.to_string(),
|
||||
// Expression::Float(v, _) => v.to_string(),
|
||||
// Expression::StringLit(s, _) => format!("\"{}\"", s),
|
||||
// Expression::Bool(b, _) => b.to_string(),
|
||||
|
||||
// Expression::BinaryOp(b) => format!(
|
||||
// "{} {} {}",
|
||||
// gen_expr(&b.left),
|
||||
// op_to_str(&b.op),
|
||||
// gen_expr(&b.right)
|
||||
// ),
|
||||
|
||||
// Expression::Call(c) => {
|
||||
// let args = c.args.iter().map(gen_expr).collect::<Vec<_>>().join(", ");
|
||||
// format!("{}({})", gen_expr(&c.callee), args)
|
||||
// }
|
||||
|
||||
// Expression::FieldAccess(f) => {
|
||||
// format!("{}.{}", gen_expr(&f.object), capitalize(&f.field))
|
||||
// }
|
||||
|
||||
// Expression::StructInit(s) => {
|
||||
// let mut out = format!("{}{{", s.name);
|
||||
|
||||
// for (i, (name, val)) in s.fields.iter().enumerate() {
|
||||
// if i > 0 {
|
||||
// out.push_str(", ");
|
||||
// }
|
||||
// out.push_str(&format!("{}: {}", capitalize(name), gen_expr(val)));
|
||||
// }
|
||||
|
||||
// out.push('}');
|
||||
// out
|
||||
// }
|
||||
|
||||
// Expression::ArrayLiteral(arr) => {
|
||||
// let elems = arr
|
||||
// .elements
|
||||
// .iter()
|
||||
// .map(gen_expr)
|
||||
// .collect::<Vec<_>>()
|
||||
// .join(", ");
|
||||
// format!("[]any{{{}}}", elems) // simple version
|
||||
// }
|
||||
|
||||
// _ => todo!(),
|
||||
// }
|
||||
// }
|
||||
|
||||
// fn gen_type(t: &TypeExpr) -> String {
|
||||
// match t {
|
||||
// TypeExpr::Named(n) => match n.as_str() {
|
||||
// "int" => "int".into(),
|
||||
// "float" => "float64".into(),
|
||||
// "string" => "string".into(),
|
||||
// _ => n.clone(),
|
||||
// },
|
||||
|
||||
// TypeExpr::Array(inner) => {
|
||||
// format!("[]{}", gen_type(inner))
|
||||
// }
|
||||
|
||||
// TypeExpr::Optional(inner) => {
|
||||
// format!("*{}", gen_type(inner)) // pointer for optional
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// fn capitalize(s: &str) -> String {
|
||||
// let mut chars = s.chars();
|
||||
// match chars.next() {
|
||||
// Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
|
||||
// None => String::new(),
|
||||
// }
|
||||
// }
|
||||
|
||||
// fn op_to_str(op: &BinOperator) -> &'static str {
|
||||
// match op {
|
||||
// BinOperator::Add => "+",
|
||||
// BinOperator::Sub => "-",
|
||||
// BinOperator::Mul => "*",
|
||||
// BinOperator::Div => "/",
|
||||
// BinOperator::Eq => "==",
|
||||
// BinOperator::NotEq => "!=",
|
||||
// BinOperator::Lt => "<",
|
||||
// BinOperator::Gt => ">",
|
||||
// BinOperator::LtEq => "<=",
|
||||
// BinOperator::GtEq => ">=",
|
||||
// BinOperator::And => "&&",
|
||||
// BinOperator::Or => "||",
|
||||
// }
|
||||
// }
|
||||
+4
-78
@@ -1,12 +1,8 @@
|
||||
pub mod compiler;
|
||||
pub mod errors;
|
||||
pub mod gogen;
|
||||
pub mod parser;
|
||||
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
|
||||
pub mod codegen;
|
||||
pub mod compiler;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
|
||||
@@ -21,21 +17,7 @@ fn main() {
|
||||
eprintln!("error: expected a file path\n usage: mist build");
|
||||
process::exit(1);
|
||||
}
|
||||
cmd_build();
|
||||
}
|
||||
"check" => {
|
||||
if args.len() < 3 {
|
||||
eprintln!("error: expected a file path\n usage: mist check <file.ms>");
|
||||
process::exit(1);
|
||||
}
|
||||
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]);
|
||||
compiler::build();
|
||||
}
|
||||
"version" | "--version" | "-v" => {
|
||||
println!("mist {}", env!("CARGO_PKG_VERSION"));
|
||||
@@ -51,62 +33,6 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cmd_build() {
|
||||
compiler::build();
|
||||
}
|
||||
|
||||
fn cmd_check(path: &str) {
|
||||
let source = read_ms_file(path);
|
||||
match parser::parse(&source) {
|
||||
Ok(_) => {
|
||||
println!("ok");
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("parse error:\n{}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
let pb = PathBuf::from(path);
|
||||
|
||||
if !pb.exists() {
|
||||
eprintln!("error: file '{}' not found", path);
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
if pb.extension().and_then(|e| e.to_str()) != Some("ms") {
|
||||
eprintln!("error: expected a .ms file, got '{}'", path);
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
fs::read_to_string(&pb).unwrap_or_else(|e| {
|
||||
eprintln!("error: could not read '{}': {}", path, e);
|
||||
process::exit(1);
|
||||
})
|
||||
}
|
||||
|
||||
fn print_usage() {
|
||||
println!("mist - the mist compiler");
|
||||
println!();
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"entry": "src/main.ms",
|
||||
"out_dir": "build"
|
||||
"src": "src",
|
||||
"output": "build"
|
||||
}
|
||||
@@ -0,0 +1,450 @@
|
||||
{
|
||||
"fmt": {
|
||||
"package_name": "fmt",
|
||||
"structs": {},
|
||||
"functions": {
|
||||
"append": {
|
||||
"export": true,
|
||||
"name": "Append",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"b": {
|
||||
"export": true,
|
||||
"var_type": "[]byte",
|
||||
"name": "b"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"[]byte"
|
||||
]
|
||||
},
|
||||
"appendf": {
|
||||
"export": true,
|
||||
"name": "Appendf",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"b": {
|
||||
"export": true,
|
||||
"var_type": "[]byte",
|
||||
"name": "b"
|
||||
},
|
||||
"format": {
|
||||
"export": true,
|
||||
"var_type": "string",
|
||||
"name": "format"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"[]byte"
|
||||
]
|
||||
},
|
||||
"appendln": {
|
||||
"export": true,
|
||||
"name": "Appendln",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"b": {
|
||||
"export": true,
|
||||
"var_type": "[]byte",
|
||||
"name": "b"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"[]byte"
|
||||
]
|
||||
},
|
||||
"errorf": {
|
||||
"export": true,
|
||||
"name": "Errorf",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"format": {
|
||||
"export": true,
|
||||
"var_type": "string",
|
||||
"name": "format"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"formatString": {
|
||||
"export": true,
|
||||
"name": "FormatString",
|
||||
"params": {
|
||||
"state": {
|
||||
"export": true,
|
||||
"var_type": "fmt.State",
|
||||
"name": "state"
|
||||
},
|
||||
"verb": {
|
||||
"export": true,
|
||||
"var_type": "rune",
|
||||
"name": "verb"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"string"
|
||||
]
|
||||
},
|
||||
"fprint": {
|
||||
"export": true,
|
||||
"name": "Fprint",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"w": {
|
||||
"export": true,
|
||||
"var_type": "io.Writer",
|
||||
"name": "w"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"fprintf": {
|
||||
"export": true,
|
||||
"name": "Fprintf",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"format": {
|
||||
"export": true,
|
||||
"var_type": "string",
|
||||
"name": "format"
|
||||
},
|
||||
"w": {
|
||||
"export": true,
|
||||
"var_type": "io.Writer",
|
||||
"name": "w"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"fprintln": {
|
||||
"export": true,
|
||||
"name": "Fprintln",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"w": {
|
||||
"export": true,
|
||||
"var_type": "io.Writer",
|
||||
"name": "w"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"fscan": {
|
||||
"export": true,
|
||||
"name": "Fscan",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"r": {
|
||||
"export": true,
|
||||
"var_type": "io.Reader",
|
||||
"name": "r"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"fscanf": {
|
||||
"export": true,
|
||||
"name": "Fscanf",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"format": {
|
||||
"export": true,
|
||||
"var_type": "string",
|
||||
"name": "format"
|
||||
},
|
||||
"r": {
|
||||
"export": true,
|
||||
"var_type": "io.Reader",
|
||||
"name": "r"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"fscanln": {
|
||||
"export": true,
|
||||
"name": "Fscanln",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"r": {
|
||||
"export": true,
|
||||
"var_type": "io.Reader",
|
||||
"name": "r"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"print": {
|
||||
"export": true,
|
||||
"name": "Print",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"printf": {
|
||||
"export": true,
|
||||
"name": "Printf",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"format": {
|
||||
"export": true,
|
||||
"var_type": "string",
|
||||
"name": "format"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"println": {
|
||||
"export": true,
|
||||
"name": "Println",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"scan": {
|
||||
"export": true,
|
||||
"name": "Scan",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"scanf": {
|
||||
"export": true,
|
||||
"name": "Scanf",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"format": {
|
||||
"export": true,
|
||||
"var_type": "string",
|
||||
"name": "format"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"scanln": {
|
||||
"export": true,
|
||||
"name": "Scanln",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"sprint": {
|
||||
"export": true,
|
||||
"name": "Sprint",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"string"
|
||||
]
|
||||
},
|
||||
"sprintf": {
|
||||
"export": true,
|
||||
"name": "Sprintf",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"format": {
|
||||
"export": true,
|
||||
"var_type": "string",
|
||||
"name": "format"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"string"
|
||||
]
|
||||
},
|
||||
"sprintln": {
|
||||
"export": true,
|
||||
"name": "Sprintln",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"string"
|
||||
]
|
||||
},
|
||||
"sscan": {
|
||||
"export": true,
|
||||
"name": "Sscan",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"str": {
|
||||
"export": true,
|
||||
"var_type": "string",
|
||||
"name": "str"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"sscanf": {
|
||||
"export": true,
|
||||
"name": "Sscanf",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"format": {
|
||||
"export": true,
|
||||
"var_type": "string",
|
||||
"name": "format"
|
||||
},
|
||||
"str": {
|
||||
"export": true,
|
||||
"var_type": "string",
|
||||
"name": "str"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
},
|
||||
"sscanln": {
|
||||
"export": true,
|
||||
"name": "Sscanln",
|
||||
"params": {
|
||||
"a": {
|
||||
"export": true,
|
||||
"var_type": "[]any",
|
||||
"name": "a"
|
||||
},
|
||||
"str": {
|
||||
"export": true,
|
||||
"var_type": "string",
|
||||
"name": "str"
|
||||
}
|
||||
},
|
||||
"return_types": [
|
||||
"int",
|
||||
"error"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package main;
|
||||
|
||||
public struct Idk {
|
||||
int a,
|
||||
int b,
|
||||
}
|
||||
|
||||
int test(int a, int b) {
|
||||
Idk bro = Idk { a: 10, b: 10 };
|
||||
return a + b + bro.a;
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
function add(a: int, b: int): int {
|
||||
let sum = 0;
|
||||
|
||||
for (let i = 0; i < 10; i = i + 1) {
|
||||
if (i == 5) {
|
||||
continue;
|
||||
}
|
||||
|
||||
sum = sum + i;
|
||||
}
|
||||
|
||||
if (sum > 20) {
|
||||
return sum;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user