comp_ctx: add FunctionSymbol.findSymbol

- add Variable symbol
This commit is contained in:
Luna 2019-09-24 23:09:00 -03:00
parent bfe9f4fe0d
commit 8afab8e4ed
1 changed files with 29 additions and 0 deletions

View File

@ -23,7 +23,23 @@ pub const SymbolUnderlyingType = union(SymbolUnderlyingTypeEnum) {
// - TODO parameters // - TODO parameters
pub const FunctionSymbol = struct { pub const FunctionSymbol = struct {
return_type: SymbolUnderlyingType, return_type: SymbolUnderlyingType,
/// Parameters for a function are also a table instead of an ArrayList
/// because we want to resolve identifiers to them.
parameters: SymbolTable,
symbols: SymbolTable, symbols: SymbolTable,
/// Find a given identifier in the function. Can resolve to either a parameter
pub fn findSymbol(self: *const FuncionSymbol, identifier: []const u8) ?Symbol {
// try to find it on overall variable symbols
var var_sym = self.symbols.get(identifier);
if (var_sym != null) return var_sym;
var param_sym = self.parameters.get(identifier);
if (param_sym != null) return param_sym;
return null;
}
}; };
// structs are hashmaps pointing to SymbolUnderlyingType // structs are hashmaps pointing to SymbolUnderlyingType
@ -37,18 +53,25 @@ pub const SymbolType = enum {
Function, Function,
Struct, Struct,
Enum, Enum,
Variable,
}; };
pub const SymbolData = union(SymbolType) { pub const SymbolData = union(SymbolType) {
Function: FunctionSymbol, Function: FunctionSymbol,
Struct: UnderlyingTypeMap, Struct: UnderlyingTypeMap,
Enum: IdentifierList, Enum: IdentifierList,
// variables (parameters and variables), for the type system
// only have types
Variable: SymbolUnderlyingType,
}; };
const builtin_type_identifiers = [_][]const u8{ "i32", "i64", "bool" }; const builtin_type_identifiers = [_][]const u8{ "i32", "i64", "bool" };
const builtin_types = [_]SymbolUnderlyingTypeEnum{ .Integer32, .Integer64, .Bool }; const builtin_types = [_]SymbolUnderlyingTypeEnum{ .Integer32, .Integer64, .Bool };
/// Represents the context for a full compiler run.
/// This is used to manage the symbol table for the compilation unit, etc.
pub const CompilationContext = struct { pub const CompilationContext = struct {
allocator: *std.mem.Allocator, allocator: *std.mem.Allocator,
symbol_table: SymbolTable, symbol_table: SymbolTable,
@ -60,6 +83,9 @@ pub const CompilationContext = struct {
}; };
} }
/// Solve a given type as a string into a SymbolUnderlyingTypeEnum
/// This does not help if you want a full SymbolUnderlyingType, use
/// solveType() for that.
pub fn solveTypeEnum( pub fn solveTypeEnum(
self: *@This(), self: *@This(),
typ_ident: []const u8, typ_ident: []const u8,
@ -71,6 +97,9 @@ pub const CompilationContext = struct {
return .CustomType; return .CustomType;
} }
/// Solve a given type string into a full fleged SymbolUnderlyingType.
/// This always works, since resolution into struct/enum custom types
/// become the fallback.
pub fn solveType( pub fn solveType(
self: *@This(), self: *@This(),
typ_ident: []const u8, typ_ident: []const u8,