From ca8a3d14744709c784c894abbe73486730582abe Mon Sep 17 00:00:00 2001 From: Dhruvan Date: Thu, 3 Oct 2024 18:24:24 -0400 Subject: [PATCH] feat(ast): enhance AST context with symbol management and context merging refactor(linked_list): update function signatures and improve import statements feat(tests): add C++ FFI import and extend generics functionality --- language/helix/pkgs/std/linked_list.hlx | 13 +++---- .../parser/ast/include/private/AST_context.hh | 37 ++++++++++++++++++- tests/main.hlx | 6 +++ 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/language/helix/pkgs/std/linked_list.hlx b/language/helix/pkgs/std/linked_list.hlx index e293e34..b85a993 100644 --- a/language/helix/pkgs/std/linked_list.hlx +++ b/language/helix/pkgs/std/linked_list.hlx @@ -10,9 +10,8 @@ // // //===------------------------------------------------------------------------------------------===// -#[link-time] -import types::{Allocator}; -import alloc::{DefaultAllocator}; +#[link-time] import types::Allocator; +#[link-time] import alloc::DefaultAllocator; class IntNode { let data: int; @@ -46,7 +45,7 @@ class IntNode { } } -fn listLength(head: *IntNode) -> int { +fn listLength(head: &IntNode) -> int { let count = 0; let *current = head; @@ -98,9 +97,9 @@ fn listRemoveNode(head: *IntNode, target: int) { } } -fn main() { - let head = IntNode(1, &null); - let *tail = &head; +fn main(a: const int, a: int | float) { + let head: IntNode = IntNode(1, &null); + let *tail = &head; for i in 2..100 { tail = tail.addNodeAfter(i); diff --git a/source/parser/ast/include/private/AST_context.hh b/source/parser/ast/include/private/AST_context.hh index ae10c2c..79a4864 100644 --- a/source/parser/ast/include/private/AST_context.hh +++ b/source/parser/ast/include/private/AST_context.hh @@ -16,8 +16,43 @@ #ifndef __AST_CONTEXT_H__ #define __AST_CONTEXT_H__ +#include #include "parser/ast/include/config/AST_config.def" +#include "parser/ast/include/private/AST_generate.hh" +#include "parser/ast/include/types/AST_types.hh" +#include "parser/ast/include/types/AST_visitor.hh" +#include "token/include/Token.hh" -__AST_BEGIN {} +__AST_BEGIN { + struct Symbol { + __AST_NODE::nodes type; ///< type of the symbol + __AST_N::NodeT<> node; ///< node that represents the symbol + __AST_N::NodeT<> parent; ///< parent node of the symbol + __TOKEN_N::Token marker; ///< token that marks the symbol + }; + + class Context { + // needs the following things: + // - the constructor takes in an AST node + // recursively traverses the AST node using 'accept' and gathers a list of symbols + // uses said symbols to build a symbol table + // - a 'find' method that takes in a string and returns a symbol + // - a 'find' method that takes in a token and returns all the matching symbols + // - a 'get' method that returns the symbol table + // - a 'get' method that takes in a parent node and returns all the symbols that are children of the parent node + // - a 'is' method that takes in a symbol and a type and returns true if the symbol is of the given type + // - a 'merge' method that takes in a Context and merges the symbol tables of the two contexts + // - a 'append' method that takes in a symbol and adds it to the symbol table + // - a 'remove' method that takes in a symbol and removes it from the symbol table + // - a 'clear' method that clears the symbol table + // - a 'size' method that returns the number of symbols in the symbol table + // felids: + // - 'symbols' is a hash map of where the std::string is name + // which is a mangled name of the symbol (guaranteed to be unique) + + private: + std::unordered_map symbols; + }; +} #endif // __AST_CONTEXT_H__ \ No newline at end of file diff --git a/tests/main.hlx b/tests/main.hlx index 3dacc9c..6a10a0e 100644 --- a/tests/main.hlx +++ b/tests/main.hlx @@ -2,6 +2,8 @@ fn main() { print("THIS IS A TEST"); } +ffi "c++" import "hello.h"; + fn test_fn() -> i32 { return 0; } @@ -25,6 +27,10 @@ class WithGenericsBound requires if T has Number { WithGenericsBound::y = 0; } + op + as fn add(self, other: T) -> T { + return self.x + other; + } + pub fn something(self, a: int, y: float = 19.21) { print("Hello, World!"); }