Removed semantics

This commit is contained in:
2026-05-01 17:40:10 +02:00
parent 3a80fa2d38
commit 533cfa29c9
9 changed files with 2 additions and 871 deletions
Generated
-10
View File
@@ -79,7 +79,6 @@ name = "mist"
version = "0.1.0"
dependencies = [
"parser",
"semantic",
"serde",
"serde_json",
]
@@ -155,15 +154,6 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "semantic"
version = "0.1.0"
dependencies = [
"parser",
"serde",
"serde_json",
]
[[package]]
name = "serde"
version = "1.0.228"
-1
View File
@@ -7,4 +7,3 @@ edition = "2024"
serde = { version = "1.0.228", features = ["serde_derive"] }
serde_json = "1.0.149"
parser = { path = "./parser" }
semantic = { path = "./semantic" }
-9
View File
@@ -1,9 +0,0 @@
[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"
-318
View File
@@ -1,318 +0,0 @@
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()
}
}
-61
View File
@@ -1,61 +0,0 @@
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::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()),
),
);
}
}
-294
View File
@@ -1,294 +0,0 @@
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;
}
}
-174
View File
@@ -1,174 +0,0 @@
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::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
}
}
+2 -2
View File
@@ -49,7 +49,7 @@ pub fn build() {
let parser_result = parser::parse(&source).map_err(|e| e.to_string());
let mut ast = match parser_result {
let ast = match parser_result {
Ok(ast) => ast,
Err(e) => {
eprintln!("error: parse failed\n{}", e);
@@ -57,7 +57,7 @@ pub fn build() {
}
};
semantic::walk_ast(semantic::scope::Scope::from_top(&root, &ast), &mut ast);
// semantic::walk_ast(semantic::scope::Scope::from_top(&root, &ast), &mut ast);
let mut gc = crate::codegen::GoCodegen::new();
let output = gc.generate(&ast);
-2
View File
@@ -1,5 +1,3 @@
package main;
public struct Idk {
int a,
int b,