This commit is contained in:
Sofia 2025-08-04 21:14:32 +03:00
parent 70a968d7a0
commit 5b7c3d5b3a
3 changed files with 42 additions and 93 deletions

View File

@ -9,6 +9,7 @@ fn main() -> i32 {
i32::memcpy(potus, otus, 1); i32::memcpy(potus, otus, 1);
printf("log10 %f\n", f64::round(123.3) as f64); printf("log10 %f\n", f64::round(123.3) as f64);
printf("sqrt %f\n", f64::sqrt(2) as f64);
printf("log10 %f\n", potus[0] as f64); printf("log10 %f\n", potus[0] as f64);
return potus[0]; return potus[0];
} }

View File

@ -192,13 +192,9 @@ impl AnalysisState {
} }
} }
pub fn new_symbol(&mut self, definition: usize, kind: SemanticKind, module_id: SourceModuleId) -> SymbolId { pub fn new_symbol(&mut self, definition: usize, kind: SemanticKind) -> SymbolId {
let id = SymbolId(self.symbol_table.len()); let id = SymbolId(self.symbol_table.len());
self.symbol_table.push(Symbol { self.symbol_table.push(Symbol { kind, definition });
kind,
definition,
module_id,
});
id id
} }
@ -223,7 +219,6 @@ impl AnalysisState {
pub struct Symbol { pub struct Symbol {
pub kind: SemanticKind, pub kind: SemanticKind,
pub definition: usize, pub definition: usize,
pub module_id: SourceModuleId,
} }
pub struct AnalysisScope<'a> { pub struct AnalysisScope<'a> {
@ -306,7 +301,6 @@ pub enum SemanticKind {
Type, Type,
Struct, Struct,
Comment, Comment,
Operator,
Keyword, Keyword,
Reference(SourceModuleId, SymbolId), Reference(SourceModuleId, SymbolId),
} }
@ -328,7 +322,6 @@ impl SemanticKind {
SemanticKind::Property => SemanticTokenType::PROPERTY, SemanticKind::Property => SemanticTokenType::PROPERTY,
SemanticKind::Struct => SemanticTokenType::STRUCT, SemanticKind::Struct => SemanticTokenType::STRUCT,
SemanticKind::Comment => SemanticTokenType::COMMENT, SemanticKind::Comment => SemanticTokenType::COMMENT,
SemanticKind::Operator => SemanticTokenType::OPERATOR,
SemanticKind::Keyword => SemanticTokenType::KEYWORD, SemanticKind::Keyword => SemanticTokenType::KEYWORD,
SemanticKind::Default => return None, SemanticKind::Default => return None,
SemanticKind::Reference(module_id, symbol_id) => { SemanticKind::Reference(module_id, symbol_id) => {
@ -360,7 +353,6 @@ impl SemanticKind {
SemanticKind::Property => SemanticTokenModifier::DECLARATION, SemanticKind::Property => SemanticTokenModifier::DECLARATION,
SemanticKind::Struct => SemanticTokenModifier::DEFINITION, SemanticKind::Struct => SemanticTokenModifier::DEFINITION,
SemanticKind::Comment => return None, SemanticKind::Comment => return None,
SemanticKind::Operator => return None,
SemanticKind::Keyword => return None, SemanticKind::Keyword => return None,
SemanticKind::Reference(..) => SEMANTIC_REFERENCE, SemanticKind::Reference(..) => SEMANTIC_REFERENCE,
}; };
@ -475,7 +467,7 @@ pub fn analyze_context(
_ => None, _ => None,
}; };
if let Some(semantic) = semantic_token { if let Some(semantic) = semantic_token {
let symbol = scope.state.new_symbol(i, semantic, module.module_id); let symbol = scope.state.new_symbol(i, semantic);
scope.state.set_symbol(i, symbol); scope.state.set_symbol(i, symbol);
} }
} }
@ -537,9 +529,7 @@ pub fn analyze_context(
let struct_idx = scope let struct_idx = scope
.token_idx(&typedef.meta, |t| matches!(t, Token::Identifier(_))) .token_idx(&typedef.meta, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(typedef.meta.range.end); .unwrap_or(typedef.meta.range.end);
let struct_symbol = scope let struct_symbol = scope.state.new_symbol(struct_idx, SemanticKind::Struct);
.state
.new_symbol(struct_idx, SemanticKind::Struct, module.module_id);
scope.state.set_symbol(struct_idx, struct_symbol); scope.state.set_symbol(struct_idx, struct_symbol);
let ty = TypeKind::CustomType(CustomTypeKey(typedef.name.clone(), typedef.source_module)); let ty = TypeKind::CustomType(CustomTypeKey(typedef.name.clone(), typedef.source_module));
@ -563,9 +553,7 @@ pub fn analyze_context(
Some(field.1.clone()), Some(field.1.clone()),
); );
let field_symbol = scope let field_symbol = scope.state.new_symbol(field_idx, SemanticKind::Property);
.state
.new_symbol(field_idx, SemanticKind::Property, module.module_id);
scope.state.set_symbol(field_idx, field_symbol); scope.state.set_symbol(field_idx, field_symbol);
scope.state.properties.insert( scope.state.properties.insert(
@ -587,7 +575,7 @@ pub fn analyze_context(
let idx = scope let idx = scope
.token_idx(&param.meta, |t| matches!(t, Token::Identifier(_))) .token_idx(&param.meta, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(param.meta.range.end); .unwrap_or(param.meta.range.end);
let symbol = scope.state.new_symbol(idx, SemanticKind::Variable, module.module_id); let symbol = scope.state.new_symbol(idx, SemanticKind::Variable);
scope.state.set_symbol(idx, symbol); scope.state.set_symbol(idx, symbol);
scope.variables.insert(param.name.clone(), symbol); scope.variables.insert(param.name.clone(), symbol);
} }
@ -617,7 +605,7 @@ pub fn analyze_context(
let idx = scope let idx = scope
.token_idx(&function.signature(), |t| matches!(t, Token::Identifier(_))) .token_idx(&function.signature(), |t| matches!(t, Token::Identifier(_)))
.unwrap_or(function.signature().range.end); .unwrap_or(function.signature().range.end);
let symbol = scope.state.new_symbol(idx, SemanticKind::Function, module.module_id); let symbol = scope.state.new_symbol(idx, SemanticKind::Function);
scope.state.set_symbol(idx, symbol); scope.state.set_symbol(idx, symbol);
scope scope
.state .state
@ -644,9 +632,7 @@ pub fn analyze_context(
let param_idx = inner_scope let param_idx = inner_scope
.token_idx(&param.meta, |t| matches!(t, Token::Identifier(_))) .token_idx(&param.meta, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(function.signature().range.end); .unwrap_or(function.signature().range.end);
let param_symbol = inner_scope let param_symbol = inner_scope.state.new_symbol(param_idx, SemanticKind::Variable);
.state
.new_symbol(param_idx, SemanticKind::Variable, module.module_id);
inner_scope.state.set_symbol(param_idx, param_symbol); inner_scope.state.set_symbol(param_idx, param_symbol);
inner_scope.variables.insert(param.name.clone(), param_symbol); inner_scope.variables.insert(param.name.clone(), param_symbol);
} }
@ -678,7 +664,7 @@ pub fn analyze_context(
let idx = scope let idx = scope
.token_idx(&function.signature(), |t| matches!(t, Token::Identifier(_))) .token_idx(&function.signature(), |t| matches!(t, Token::Identifier(_)))
.unwrap_or(function.signature().range.end); .unwrap_or(function.signature().range.end);
let function_symbol = scope.state.new_symbol(idx, SemanticKind::Function, module.module_id); let function_symbol = scope.state.new_symbol(idx, SemanticKind::Function);
scope.state.set_symbol(idx, function_symbol); scope.state.set_symbol(idx, function_symbol);
scope.state.functions.insert(function.name.clone(), function_symbol); scope.state.functions.insert(function.name.clone(), function_symbol);
scope scope
@ -701,9 +687,7 @@ pub fn analyze_context(
let idx = inner_scope let idx = inner_scope
.token_idx(&param.meta, |t| matches!(t, Token::Identifier(_))) .token_idx(&param.meta, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(function.signature().range.end); .unwrap_or(function.signature().range.end);
let symbol = inner_scope let symbol = inner_scope.state.new_symbol(idx, SemanticKind::Variable);
.state
.new_symbol(idx, SemanticKind::Variable, module.module_id);
inner_scope.state.set_symbol(idx, symbol); inner_scope.state.set_symbol(idx, symbol);
inner_scope.variables.insert(param.name.clone(), symbol); inner_scope.variables.insert(param.name.clone(), symbol);
} }
@ -744,9 +728,7 @@ pub fn analyze_block(
let idx = scope let idx = scope
.token_idx(&named_variable_ref.2, |t| matches!(t, Token::Identifier(_))) .token_idx(&named_variable_ref.2, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(named_variable_ref.2.range.end); .unwrap_or(named_variable_ref.2.range.end);
let symbol = scope let symbol = scope.state.new_symbol(idx, SemanticKind::Variable);
.state
.new_symbol(idx, SemanticKind::Variable, source_module.module_id);
scope.state.set_symbol(idx, symbol); scope.state.set_symbol(idx, symbol);
scope.variables.insert(named_variable_ref.1.clone(), symbol); scope.variables.insert(named_variable_ref.1.clone(), symbol);
@ -794,13 +776,11 @@ pub fn analyze_expr(
.token_idx(&var_ref.2, |t| matches!(t, Token::Identifier(_))) .token_idx(&var_ref.2, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(var_ref.2.range.end); .unwrap_or(var_ref.2.range.end);
let symbol = if let Some(symbol_id) = scope.variables.get(&var_ref.1) { let symbol = if let Some(symbol_id) = scope.variables.get(&var_ref.1) {
scope.state.new_symbol( scope
idx, .state
SemanticKind::Reference(source_module.module_id, *symbol_id), .new_symbol(idx, SemanticKind::Reference(source_module.module_id, *symbol_id))
source_module.module_id,
)
} else { } else {
scope.state.new_symbol(idx, SemanticKind::Type, source_module.module_id) scope.state.new_symbol(idx, SemanticKind::Type)
}; };
scope.state.set_symbol(idx, symbol); scope.state.set_symbol(idx, symbol);
} }
@ -840,15 +820,11 @@ pub fn analyze_expr(
let field_symbol = if let Some((module_id, symbol_id)) = let field_symbol = if let Some((module_id, symbol_id)) =
scope.find_property(accessed_type.clone(), name.clone()) scope.find_property(accessed_type.clone(), name.clone())
{ {
scope.state.new_symbol(
field_idx,
SemanticKind::Reference(module_id, symbol_id),
source_module.module_id,
)
} else {
scope scope
.state .state
.new_symbol(field_idx, SemanticKind::Property, source_module.module_id) .new_symbol(field_idx, SemanticKind::Reference(module_id, symbol_id))
} else {
scope.state.new_symbol(field_idx, SemanticKind::Property)
}; };
scope.state.set_symbol(field_idx, field_symbol); scope.state.set_symbol(field_idx, field_symbol);
@ -883,15 +859,11 @@ pub fn analyze_expr(
.unwrap_or(expr.1.range.end); .unwrap_or(expr.1.range.end);
let struct_symbol = if let Some(symbol_id) = scope.state.types.get(&struct_type) { let struct_symbol = if let Some(symbol_id) = scope.state.types.get(&struct_type) {
scope.state.new_symbol(
struct_idx,
SemanticKind::Reference(source_module.module_id, *symbol_id),
source_module.module_id,
)
} else {
scope scope
.state .state
.new_symbol(struct_idx, SemanticKind::Struct, source_module.module_id) .new_symbol(struct_idx, SemanticKind::Reference(source_module.module_id, *symbol_id))
} else {
scope.state.new_symbol(struct_idx, SemanticKind::Struct)
}; };
scope.state.set_symbol(struct_idx, struct_symbol); scope.state.set_symbol(struct_idx, struct_symbol);
@ -902,15 +874,11 @@ pub fn analyze_expr(
let field_symbol = let field_symbol =
if let Some(symbol_id) = scope.state.properties.get(&(struct_type.clone(), field_name.clone())) { if let Some(symbol_id) = scope.state.properties.get(&(struct_type.clone(), field_name.clone())) {
scope.state.new_symbol(
field_idx,
SemanticKind::Reference(source_module.module_id, *symbol_id),
source_module.module_id,
)
} else {
scope scope
.state .state
.new_symbol(field_idx, SemanticKind::Property, source_module.module_id) .new_symbol(field_idx, SemanticKind::Reference(source_module.module_id, *symbol_id))
} else {
scope.state.new_symbol(field_idx, SemanticKind::Property)
}; };
scope.state.set_symbol(field_idx, field_symbol); scope.state.set_symbol(field_idx, field_symbol);
@ -920,18 +888,14 @@ pub fn analyze_expr(
} }
mir::ExprKind::Literal(_) => { mir::ExprKind::Literal(_) => {
if let Some(idx) = scope.token_idx(&expr.1, |t| matches!(t, Token::StringLit(_) | Token::CharLit(_))) { if let Some(idx) = scope.token_idx(&expr.1, |t| matches!(t, Token::StringLit(_) | Token::CharLit(_))) {
scope scope.state.new_symbol(idx, SemanticKind::String);
.state
.new_symbol(idx, SemanticKind::String, source_module.module_id);
} else if let Some(idx) = scope.token_idx(&expr.1, |t| { } else if let Some(idx) = scope.token_idx(&expr.1, |t| {
matches!( matches!(
t, t,
Token::DecimalValue(_) | Token::HexadecimalValue(_) | Token::OctalValue(_) | Token::BinaryValue(_) Token::DecimalValue(_) | Token::HexadecimalValue(_) | Token::OctalValue(_) | Token::BinaryValue(_)
) )
}) { }) {
scope scope.state.new_symbol(idx, SemanticKind::Number);
.state
.new_symbol(idx, SemanticKind::Number, source_module.module_id);
} }
} }
mir::ExprKind::BinOp(_, lhs, rhs, _) => { mir::ExprKind::BinOp(_, lhs, rhs, _) => {
@ -949,15 +913,11 @@ pub fn analyze_expr(
.token_idx(&meta, |t| matches!(t, Token::Identifier(_))) .token_idx(&meta, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(meta.range.end); .unwrap_or(meta.range.end);
let symbol = if let Some((module_id, symbol_id)) = scope.functions.get(name) { let symbol = if let Some((module_id, symbol_id)) = scope.functions.get(name) {
scope.state.new_symbol(
idx,
SemanticKind::Reference(*module_id, *symbol_id),
source_module.module_id,
)
} else {
scope scope
.state .state
.new_symbol(idx, SemanticKind::Function, source_module.module_id) .new_symbol(idx, SemanticKind::Reference(*module_id, *symbol_id))
} else {
scope.state.new_symbol(idx, SemanticKind::Function)
}; };
scope.state.set_symbol(idx, symbol); scope.state.set_symbol(idx, symbol);
} }
@ -977,15 +937,11 @@ pub fn analyze_expr(
}; };
let type_symbol = if let Some((module_id, symbol_id)) = scope.types.get(&invoked_ty) { let type_symbol = if let Some((module_id, symbol_id)) = scope.types.get(&invoked_ty) {
scope.state.new_symbol(
type_idx,
SemanticKind::Reference(*module_id, *symbol_id),
source_module.module_id,
)
} else {
scope scope
.state .state
.new_symbol(type_idx, SemanticKind::Type, source_module.module_id) .new_symbol(type_idx, SemanticKind::Reference(*module_id, *symbol_id))
} else {
scope.state.new_symbol(type_idx, SemanticKind::Type)
}; };
scope.state.set_symbol(type_idx, type_symbol); scope.state.set_symbol(type_idx, type_symbol);
@ -995,15 +951,11 @@ pub fn analyze_expr(
let fn_symbol = if let Some((module_id, symbol_id)) = let fn_symbol = if let Some((module_id, symbol_id)) =
scope.associated_functions.get(&(invoked_ty.clone(), name.clone())) scope.associated_functions.get(&(invoked_ty.clone(), name.clone()))
{ {
scope.state.new_symbol(
fn_idx,
SemanticKind::Reference(*module_id, *symbol_id),
source_module.module_id,
)
} else {
scope scope
.state .state
.new_symbol(fn_idx, SemanticKind::Function, source_module.module_id) .new_symbol(fn_idx, SemanticKind::Reference(*module_id, *symbol_id))
} else {
scope.state.new_symbol(fn_idx, SemanticKind::Function)
}; };
scope.state.set_symbol(fn_idx, fn_symbol); scope.state.set_symbol(fn_idx, fn_symbol);

View File

@ -20,7 +20,6 @@ fn main() -> Result<(), std::io::Error> {
let mir_path = parent.with_extension("mir"); let mir_path = parent.with_extension("mir");
let asm_path = parent.with_extension("asm"); let asm_path = parent.with_extension("asm");
#[cfg(feature = "log_output")]
let before = std::time::SystemTime::now(); let before = std::time::SystemTime::now();
let text = fs::read_to_string(&path)?; let text = fs::read_to_string(&path)?;
@ -54,16 +53,13 @@ fn main() -> Result<(), std::io::Error> {
fs::write(&object_path, &obj_buffer).expect("Could not write Object-file!"); fs::write(&object_path, &obj_buffer).expect("Could not write Object-file!");
fs::write(&llir_path, &llir).expect("Could not write LLIR-file!"); fs::write(&llir_path, &llir).expect("Could not write LLIR-file!");
fs::write(&mir_path, &mir).expect("Could not write MIR-file!"); fs::write(&mir_path, &mir).expect("Could not write MIR-file!");
#[cfg(feature = "log_output")] let after = std::time::SystemTime::now();
{ println!(
let after = std::time::SystemTime::now(); "Compilation took: {:.2}ms\n",
println!( (after.duration_since(before).unwrap().as_micros() as f32) / 1000.
"Compilation took: {:.2}ms\n", );
(after.duration_since(before).unwrap().as_micros() as f32) / 1000.
);
println!("Linking {:?}", &object_path); println!("Linking {:?}", &object_path);
}
let linker = std::env::var("LD").unwrap_or("ld".to_owned()); let linker = std::env::var("LD").unwrap_or("ld".to_owned());
let mut linker = LDRunner::from_command(&linker).with_library("c").with_library("m"); let mut linker = LDRunner::from_command(&linker).with_library("c").with_library("m");