Skip to content

Commit

Permalink
feat(ast): enhance AST context with symbol management and context mer…
Browse files Browse the repository at this point in the history
…ging

refactor(linked_list): update function signatures and improve import statements
feat(tests): add C++ FFI import and extend generics functionality
  • Loading branch information
Ze7111 committed Oct 3, 2024
1 parent 2ea0948 commit ca8a3d1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
13 changes: 6 additions & 7 deletions language/helix/pkgs/std/linked_list.hlx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,7 +45,7 @@ class IntNode {
}
}

fn listLength(head: *IntNode) -> int {
fn listLength(head: &IntNode) -> int {
let count = 0;
let *current = head;

Expand Down Expand Up @@ -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);
Expand Down
37 changes: 36 additions & 1 deletion source/parser/ast/include/private/AST_context.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,43 @@
#ifndef __AST_CONTEXT_H__
#define __AST_CONTEXT_H__

#include <unordered_map>
#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 <std::string, Symbol> where the std::string is name
// which is a mangled name of the symbol (guaranteed to be unique)

private:
std::unordered_map<std::string, Symbol> symbols;
};
}

#endif // __AST_CONTEXT_H__
6 changes: 6 additions & 0 deletions tests/main.hlx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ fn main() {
print("THIS IS A TEST");
}

ffi "c++" import "hello.h";

fn test_fn() -> i32 {
return 0;
}
Expand All @@ -25,6 +27,10 @@ class WithGenericsBound requires <T> 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!");
}
Expand Down

0 comments on commit ca8a3d1

Please sign in to comment.