field exporting
This commit is contained in:
+1
-7
@@ -3,7 +3,7 @@ use std::collections::HashMap;
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub struct ParamList(pub Vec<(String, TypeExpr)>);
|
pub struct ParamList(pub HashMap<String, (bool, TypeExpr)>);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub struct Block(pub Vec<Statement>);
|
pub struct Block(pub Vec<Statement>);
|
||||||
@@ -120,9 +120,3 @@ pub enum VarKind {
|
|||||||
Const,
|
Const,
|
||||||
Var,
|
Var,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParamList {
|
|
||||||
pub fn to_hashmap<T>(&self, f: impl Fn(&String, &TypeExpr) -> T) -> HashMap<String, T> {
|
|
||||||
self.0.iter().map(|(k, v)| (k.clone(), f(k, v))).collect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export = { "export" }
|
|||||||
import = { "import" ~ string_lit ~ ";" }
|
import = { "import" ~ string_lit ~ ";" }
|
||||||
|
|
||||||
param_list = { param ~ ("," ~ param)* ~ ","? }
|
param_list = { param ~ ("," ~ param)* ~ ","? }
|
||||||
param = { identifier ~ ":" ~ type_expr }
|
param = { export? ~ identifier ~ ":" ~ type_expr }
|
||||||
|
|
||||||
struct_decl = {
|
struct_decl = {
|
||||||
export? ~ "struct" ~ identifier ~ "{" ~ param_list? ~ "}"
|
export? ~ "struct" ~ identifier ~ "{" ~ param_list? ~ "}"
|
||||||
|
|||||||
+14
-5
@@ -1,3 +1,5 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use pest::Parser;
|
use pest::Parser;
|
||||||
use pest_derive::Parser;
|
use pest_derive::Parser;
|
||||||
|
|
||||||
@@ -39,14 +41,21 @@ impl TypeExpr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ParamList {
|
impl ParamList {
|
||||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
|
pub fn from_pair(export_allowed: bool, pair: pest::iterators::Pair<Rule>) -> Self {
|
||||||
let params = pair
|
let params = pair
|
||||||
.into_inner()
|
.into_inner()
|
||||||
.map(|p| {
|
.map(|p| {
|
||||||
let mut param_inner = p.into_inner();
|
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_name = param_inner.next().unwrap().as_str().to_string();
|
||||||
let param_type = TypeExpr::from_pair(param_inner.next().unwrap());
|
let param_type = TypeExpr::from_pair(param_inner.next().unwrap());
|
||||||
(param_name, param_type)
|
(param_name, (export, param_type))
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@@ -76,9 +85,9 @@ impl TopLevel {
|
|||||||
};
|
};
|
||||||
let name = inner.next().unwrap().as_str().to_string();
|
let name = inner.next().unwrap().as_str().to_string();
|
||||||
let params = if inner.peek().unwrap().as_rule() == Rule::param_list {
|
let params = if inner.peek().unwrap().as_rule() == Rule::param_list {
|
||||||
ParamList::from_pair(inner.next().unwrap())
|
ParamList::from_pair(false, inner.next().unwrap())
|
||||||
} else {
|
} else {
|
||||||
ParamList(vec![])
|
ParamList(HashMap::new())
|
||||||
};
|
};
|
||||||
let return_type = if let Some(next) = inner.peek() {
|
let return_type = if let Some(next) = inner.peek() {
|
||||||
if next.as_rule() == Rule::type_expr {
|
if next.as_rule() == Rule::type_expr {
|
||||||
@@ -115,7 +124,7 @@ impl TopLevel {
|
|||||||
};
|
};
|
||||||
let name = inner.next().unwrap().as_str().to_string();
|
let name = inner.next().unwrap().as_str().to_string();
|
||||||
let fields_pair = inner.next().unwrap();
|
let fields_pair = inner.next().unwrap();
|
||||||
let fields = ParamList::from_pair(fields_pair);
|
let fields = ParamList::from_pair(true, fields_pair);
|
||||||
|
|
||||||
Some(TopLevel::StructDecl {
|
Some(TopLevel::StructDecl {
|
||||||
export,
|
export,
|
||||||
|
|||||||
+8
-4
@@ -48,12 +48,16 @@ pub fn walk_ast(top_scope: Arc<Scope>, tl: &mut Vec<parser::ast::TopLevel>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn walk_param_list(fields: &HashMap<String, Arc<VarRef>>, param_list: &mut ParamList) {
|
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();
|
param_list.0.clear();
|
||||||
|
|
||||||
for (_, param) in fields {
|
for (name, param) in fields {
|
||||||
param_list.0.push((
|
param_list.0.insert(
|
||||||
param.name.clone(),
|
param.name.clone(),
|
||||||
TypeExpr::Identifier(param.var_type.get_name()),
|
(
|
||||||
));
|
old_param_list.0.get(name).map(|a| a.0).unwrap_or_default(),
|
||||||
|
TypeExpr::Identifier(param.var_type.get_name()),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -151,7 +151,7 @@ impl LocalScope {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_params(self: &Arc<Self>, param_list: &ParamList) {
|
pub fn with_params(self: &Arc<Self>, param_list: &ParamList) {
|
||||||
for (param_name, type_expr) in ¶m_list.0 {
|
for (param_name, (_, type_expr)) in ¶m_list.0 {
|
||||||
match type_expr {
|
match type_expr {
|
||||||
parser::ast::TypeExpr::Identifier(id) => {
|
parser::ast::TypeExpr::Identifier(id) => {
|
||||||
if let Some(var_type) =
|
if let Some(var_type) =
|
||||||
|
|||||||
+30
-12
@@ -47,12 +47,21 @@ impl FunctionSymbol {
|
|||||||
Self {
|
Self {
|
||||||
export: export,
|
export: export,
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
params: params.to_hashmap(|name, v| VarSymbol {
|
params: params
|
||||||
name: name.clone(),
|
.0
|
||||||
var_type: TypeSymbol(match v {
|
.iter()
|
||||||
TypeExpr::Identifier(i) => i.to_string(),
|
.map(|(name, (_, v))| {
|
||||||
}),
|
(
|
||||||
}),
|
name.clone(),
|
||||||
|
VarSymbol {
|
||||||
|
name: name.clone(),
|
||||||
|
var_type: TypeSymbol(match v {
|
||||||
|
TypeExpr::Identifier(i) => i.to_string(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
return_type: return_type.map(TypeSymbol::from_ast),
|
return_type: return_type.map(TypeSymbol::from_ast),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,12 +72,21 @@ impl StructSymbol {
|
|||||||
Self {
|
Self {
|
||||||
export,
|
export,
|
||||||
name,
|
name,
|
||||||
fields: fields.to_hashmap(|name, v| VarSymbol {
|
fields: fields
|
||||||
name: name.clone(),
|
.0
|
||||||
var_type: TypeSymbol(match v {
|
.iter()
|
||||||
TypeExpr::Identifier(i) => i.to_string(),
|
.map(|(name, (_, v))| {
|
||||||
}),
|
(
|
||||||
}),
|
name.clone(),
|
||||||
|
VarSymbol {
|
||||||
|
name: name.clone(),
|
||||||
|
var_type: TypeSymbol(match v {
|
||||||
|
TypeExpr::Identifier(i) => i.to_string(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
// TODO - parse struct methods
|
// TODO - parse struct methods
|
||||||
methods: HashMap::new(),
|
methods: HashMap::new(),
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
export struct MyType {
|
export struct MyType {
|
||||||
x: int,
|
export x: int,
|
||||||
}
|
}
|
||||||
|
|
||||||
export function test(a: MyType) { }
|
export function test(a: MyType) { }
|
||||||
|
|||||||
Reference in New Issue
Block a user