+1
-12
@@ -3,13 +3,12 @@ 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),
|
||||
}
|
||||
@@ -30,7 +29,6 @@ pub enum BinaryOp {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum TopLevel {
|
||||
Import(String),
|
||||
StructDecl {
|
||||
@@ -48,7 +46,6 @@ pub enum TopLevel {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum Postfix {
|
||||
FieldAccess(String),
|
||||
Call(Vec<Expression>),
|
||||
@@ -57,7 +54,6 @@ pub enum Postfix {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum Statement {
|
||||
// expr;
|
||||
Expression(Expression),
|
||||
@@ -106,7 +102,6 @@ pub enum Statement {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum Expression {
|
||||
Identifier(String),
|
||||
IntLiteral(i64),
|
||||
@@ -125,9 +120,3 @@ pub enum VarKind {
|
||||
Const,
|
||||
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 ~ ";" }
|
||||
|
||||
param_list = { param ~ ("," ~ param)* ~ ","? }
|
||||
param = { identifier ~ ":" ~ type_expr }
|
||||
param = { export? ~ identifier ~ ":" ~ type_expr }
|
||||
|
||||
struct_decl = {
|
||||
export? ~ "struct" ~ identifier ~ "{" ~ param_list? ~ "}"
|
||||
|
||||
+14
-7
@@ -1,3 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use pest::Parser;
|
||||
use pest_derive::Parser;
|
||||
|
||||
@@ -39,14 +41,21 @@ impl TypeExpr {
|
||||
}
|
||||
|
||||
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
|
||||
.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)
|
||||
(param_name, (export, param_type))
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -76,9 +85,9 @@ impl TopLevel {
|
||||
};
|
||||
let name = inner.next().unwrap().as_str().to_string();
|
||||
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 {
|
||||
ParamList(vec![])
|
||||
ParamList(HashMap::new())
|
||||
};
|
||||
let return_type = if let Some(next) = inner.peek() {
|
||||
if next.as_rule() == Rule::type_expr {
|
||||
@@ -115,7 +124,7 @@ 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_pair(true, fields_pair);
|
||||
|
||||
Some(TopLevel::StructDecl {
|
||||
export,
|
||||
@@ -167,8 +176,6 @@ impl Statement {
|
||||
let kind_pair = inner.next().unwrap(); // let/const/var
|
||||
let name_pair = inner.next().unwrap(); // identifier
|
||||
|
||||
println!("{:#?}", inner);
|
||||
|
||||
let init = inner.next().map(Expression::from_pair);
|
||||
|
||||
let kind = match kind_pair.as_str() {
|
||||
|
||||
+40
-29
@@ -17,6 +17,7 @@ pub enum TypeRef {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct VarRef {
|
||||
pub export: bool,
|
||||
pub var_type: Arc<TypeRef>,
|
||||
pub name: String,
|
||||
}
|
||||
@@ -82,6 +83,7 @@ impl TopLevelHirScope {
|
||||
self.variables.insert(
|
||||
symbol.name.clone(),
|
||||
Arc::new(VarRef {
|
||||
export: false,
|
||||
name: name.clone(),
|
||||
var_type: Arc::new(TypeRef::Function(rf)),
|
||||
}),
|
||||
@@ -97,33 +99,39 @@ impl TopLevelHirScope {
|
||||
tlss: &TopLevelSymbolScope,
|
||||
symbol: &StructSymbol,
|
||||
) -> Arc<TypeRef> {
|
||||
let name = self.get_name(symbol.export);
|
||||
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 {
|
||||
let rf = Arc::new(TypeRef::Struct(StructRef {
|
||||
export: symbol.export,
|
||||
name: name.clone(),
|
||||
var_type: rf.clone(),
|
||||
}),
|
||||
);
|
||||
fields: symbol
|
||||
.fields
|
||||
.iter()
|
||||
.map(|(name, v)| (name.clone(), self.var_ref(tlss, v)))
|
||||
.collect(),
|
||||
}));
|
||||
|
||||
rf
|
||||
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: symbol.name.clone(),
|
||||
name: self.get_name(symbol.export),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -132,19 +140,12 @@ impl TopLevelHirScope {
|
||||
var_ref.var_type.clone()
|
||||
} else {
|
||||
if let Some(tlss_rf) = tlss.structs.get(&symbol.0) {
|
||||
let struct_ref = self.struct_ref(tlss, tlss_rf);
|
||||
|
||||
let var_ref = Arc::new(VarRef {
|
||||
name: symbol.0.clone(),
|
||||
var_type: struct_ref,
|
||||
});
|
||||
self.variables.insert(symbol.0.clone(), var_ref.clone());
|
||||
|
||||
var_ref.var_type.clone()
|
||||
self.struct_ref(tlss, tlss_rf)
|
||||
} else {
|
||||
match symbol.0.as_str() {
|
||||
"int" => {
|
||||
let var_ref = Arc::new(VarRef {
|
||||
export: false,
|
||||
name: symbol.0.clone(),
|
||||
var_type: Arc::new(TypeRef::Int),
|
||||
});
|
||||
@@ -167,10 +168,20 @@ impl TopLevelHirScope {
|
||||
}
|
||||
|
||||
pub fn next_var_idx(&self) -> usize {
|
||||
self.var_idx.fetch_add(1, Ordering::Relaxed) + 1
|
||||
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::Int => "int".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+42
-4
@@ -1,6 +1,11 @@
|
||||
use std::sync::Arc;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use crate::scope::{LocalScope, Scope};
|
||||
use parser::ast::{ParamList, TypeExpr};
|
||||
|
||||
use crate::{
|
||||
hir::VarRef,
|
||||
scope::{LocalScope, Scope},
|
||||
};
|
||||
|
||||
pub mod hir;
|
||||
pub mod scope;
|
||||
@@ -11,7 +16,17 @@ pub fn walk_ast(top_scope: Arc<Scope>, tl: &mut Vec<parser::ast::TopLevel>) {
|
||||
match tl {
|
||||
parser::ast::TopLevel::Import(_) => unimplemented!(),
|
||||
|
||||
parser::ast::TopLevel::FunctionDecl { params, body, .. } => {
|
||||
parser::ast::TopLevel::FunctionDecl {
|
||||
params, body, name, ..
|
||||
} => {
|
||||
let rf = top_scope.get_reference(name).unwrap();
|
||||
*name = rf.name.clone();
|
||||
|
||||
match &*rf.var_type {
|
||||
hir::TypeRef::Function(f) => walk_param_list(&f.params, params),
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
|
||||
let scope = LocalScope::new(top_scope.clone());
|
||||
|
||||
scope.with_params(params);
|
||||
@@ -19,7 +34,30 @@ pub fn walk_ast(top_scope: Arc<Scope>, tl: &mut Vec<parser::ast::TopLevel>) {
|
||||
scope.with_block(body);
|
||||
}
|
||||
|
||||
parser::ast::TopLevel::StructDecl { .. } => {}
|
||||
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()),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+17
-6
@@ -71,6 +71,7 @@ impl LocalScope {
|
||||
self.variables.lock().unwrap().insert(
|
||||
name.clone(),
|
||||
Arc::new(VarRef {
|
||||
export: false,
|
||||
name: name.to_string(),
|
||||
var_type,
|
||||
}),
|
||||
@@ -84,8 +85,8 @@ impl LocalScope {
|
||||
|
||||
pub fn walk_postfixes(
|
||||
self: &Arc<Self>,
|
||||
initial: &Box<ast::Expression>,
|
||||
postfixes: &Vec<Postfix>,
|
||||
initial: &mut Box<ast::Expression>,
|
||||
postfixes: &mut Vec<Postfix>,
|
||||
) -> Option<Arc<TypeRef>> {
|
||||
let mut current_type = self.get_type_from_expr(initial)?;
|
||||
|
||||
@@ -93,7 +94,9 @@ impl LocalScope {
|
||||
match postfix {
|
||||
Postfix::FieldAccess(id) => match &*current_type {
|
||||
TypeRef::Struct(s) => {
|
||||
current_type = s.fields.get(id)?.var_type.clone();
|
||||
let field = s.fields.get(id)?;
|
||||
*id = field.name.clone();
|
||||
current_type = field.var_type.clone();
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
},
|
||||
@@ -111,7 +114,10 @@ impl LocalScope {
|
||||
Some(current_type)
|
||||
}
|
||||
|
||||
pub fn get_type_from_expr(self: &Arc<Self>, expr: &ast::Expression) -> Option<Arc<TypeRef>> {
|
||||
pub fn get_type_from_expr(
|
||||
self: &Arc<Self>,
|
||||
expr: &mut ast::Expression,
|
||||
) -> Option<Arc<TypeRef>> {
|
||||
match expr {
|
||||
ast::Expression::IntLiteral(_) => self
|
||||
.parent
|
||||
@@ -133,7 +139,11 @@ impl LocalScope {
|
||||
.get_reference(&"string".to_string())
|
||||
.map(|r| r.var_type.clone()),
|
||||
|
||||
ast::Expression::Identifier(id) => self.get_reference(id).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)
|
||||
@@ -142,7 +152,7 @@ impl LocalScope {
|
||||
}
|
||||
|
||||
pub fn with_params(self: &Arc<Self>, param_list: &ParamList) {
|
||||
for (param_name, type_expr) in ¶m_list.0 {
|
||||
for (param_name, (export, type_expr)) in ¶m_list.0 {
|
||||
match type_expr {
|
||||
parser::ast::TypeExpr::Identifier(id) => {
|
||||
if let Some(var_type) =
|
||||
@@ -151,6 +161,7 @@ impl LocalScope {
|
||||
self.variables.lock().unwrap().insert(
|
||||
param_name.clone(),
|
||||
Arc::new(VarRef {
|
||||
export: *export,
|
||||
var_type,
|
||||
name: param_name.clone(),
|
||||
}),
|
||||
|
||||
+33
-12
@@ -9,6 +9,7 @@ pub struct TypeSymbol(pub String);
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct VarSymbol {
|
||||
pub export: bool,
|
||||
pub var_type: TypeSymbol,
|
||||
pub name: String,
|
||||
}
|
||||
@@ -47,12 +48,22 @@ impl FunctionSymbol {
|
||||
Self {
|
||||
export: export,
|
||||
name: name.clone(),
|
||||
params: params.to_hashmap(|name, v| VarSymbol {
|
||||
name: name.clone(),
|
||||
var_type: TypeSymbol(match v {
|
||||
TypeExpr::Identifier(i) => i.to_string(),
|
||||
}),
|
||||
}),
|
||||
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),
|
||||
}
|
||||
}
|
||||
@@ -63,12 +74,22 @@ impl StructSymbol {
|
||||
Self {
|
||||
export,
|
||||
name,
|
||||
fields: fields.to_hashmap(|name, v| VarSymbol {
|
||||
name: name.clone(),
|
||||
var_type: TypeSymbol(match v {
|
||||
TypeExpr::Identifier(i) => i.to_string(),
|
||||
}),
|
||||
}),
|
||||
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(),
|
||||
}
|
||||
|
||||
+9
-9
@@ -72,15 +72,15 @@ fn cmd_check(path: &str) {
|
||||
fn cmd_parse(path: &str) {
|
||||
let source = read_ms_file(path);
|
||||
match parser::parse(&source) {
|
||||
Ok(output) => {
|
||||
fs::write(
|
||||
"output.json",
|
||||
serde_json::to_string_pretty(&output).unwrap(),
|
||||
)
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("error: could not write output.json: {}", e);
|
||||
process::exit(1);
|
||||
});
|
||||
Ok(mut ast) => {
|
||||
walk_ast(semantic::scope::Scope::from_top(&ast), &mut ast);
|
||||
|
||||
fs::write("output.json", serde_json::to_string_pretty(&ast).unwrap()).unwrap_or_else(
|
||||
|e| {
|
||||
eprintln!("error: could not write output.json: {}", e);
|
||||
process::exit(1);
|
||||
},
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("parse error:\n{}", e);
|
||||
|
||||
+3
-7
@@ -1,9 +1,5 @@
|
||||
struct Test {
|
||||
x: int
|
||||
export struct MyType {
|
||||
export x: int,
|
||||
}
|
||||
|
||||
function add() {
|
||||
let output = test();
|
||||
}
|
||||
|
||||
function test(): int { }
|
||||
export function test(a: MyType) { }
|
||||
|
||||
Reference in New Issue
Block a user