From 8afab8e4eddca710a0b5d6abb81f53c8cc4f9905 Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 24 Sep 2019 23:09:00 -0300 Subject: [PATCH] comp_ctx: add FunctionSymbol.findSymbol - add Variable symbol --- src/comp_ctx.zig | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/comp_ctx.zig b/src/comp_ctx.zig index 79e5116..d8888c2 100644 --- a/src/comp_ctx.zig +++ b/src/comp_ctx.zig @@ -23,7 +23,23 @@ pub const SymbolUnderlyingType = union(SymbolUnderlyingTypeEnum) { // - TODO parameters pub const FunctionSymbol = struct { 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, + + /// 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 @@ -37,18 +53,25 @@ pub const SymbolType = enum { Function, Struct, Enum, + Variable, }; pub const SymbolData = union(SymbolType) { Function: FunctionSymbol, Struct: UnderlyingTypeMap, 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_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 { allocator: *std.mem.Allocator, 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( self: *@This(), typ_ident: []const u8, @@ -71,6 +97,9 @@ pub const CompilationContext = struct { 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( self: *@This(), typ_ident: []const u8,