Skip to content

Commit

Permalink
Merge pull request #41 from kneorain/ast-parser-v5
Browse files Browse the repository at this point in the history
Finally Completed - 90% of the Ast Parser **v5**
  • Loading branch information
Ze7111 authored Oct 3, 2024
2 parents 495518b + c0e741e commit 919e14d
Show file tree
Hide file tree
Showing 107 changed files with 255,220 additions and 3,347 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "libs/llvm-18.1.9-src"]
path = libs/llvm-18.1.9-src
url = https://github.com/kneorain/llvm-project
[submodule "libs/PEGTL"]
path = libs/PEGTL
url = https://github.com/taocpp/PEGTL
28 changes: 24 additions & 4 deletions helix.bat
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
@echo off
setlocal

rem Build the helix project using xmake
xmake
rem Initialize an empty variable to collect the remaining arguments
set "ARGS="

rem Run the helix project with all arguments passed to the batch file
build\debug\x64-msvc-windows\bin\helix %*
rem Check if the first argument is '--'
if "%1" == "--" (
rem Shift to remove the '--'
shift
) else (
rem Build the Helix project using xmake
xmake
)

rem Collect all the remaining arguments after '--'
:loop
if "%1"=="" goto endloop
set "ARGS=%ARGS% %1"
shift
goto loop

:endloop
rem Now run the binary with the collected arguments, without passing '--'
build\debug\x64-msvc-windows\bin\helix %ARGS%

endlocal
514 changes: 514 additions & 0 deletions language/.hlx

Large diffs are not rendered by default.

71 changes: 66 additions & 5 deletions language/all_keyword_usage.hlx
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,69 @@ const for i in 0:10 {
}


class Point {
let static n_points = 0;

static const fn get_n_points() -> int {
return Point::n_points;
}


static fn add_point() {
Point::n_points += 1;
}
}


// HARD ERROR
inline const async panic fn div_by_0_hard_a(a: int, b: int = 0) -> int { // force try catch handling
return a / b;
}

// HARD ERROR - Not forced to handle
fn div_by_0_hard_b(a: int, b: int = 0) -> int {
return a / b;
}

// Error as value
fn div_by_0_soft(a: int, b: int = 0) -> Result<int, DivByZeroError> {
if b == 0 {
return DivByZeroError();
}

return a / b;
}

fn main() {
// SOFT ERROR
let const a = div_by_0_soft(10, 0);

if a.is_err() {
print("DivByZeroError");
} else {
let a = a.unwrap();
}

// HARD ERROR
try {
let b = div_by_0_hard_a(10, 0);
} catch (err: DivByZeroError) {
print("DivByZeroError");
}

// or

let b = try div_by_0_hard_a(10, 0) catch (err: DivByZeroError) 10 catch (err: BaseError) 9;

let c = div_by_0_hard_b(10, 0); // not forced to handle but you can if you want
}






/// 11
/// 11
while true {
break;
}
Expand Down Expand Up @@ -136,7 +194,7 @@ return (
)(a, b);
)(a, b);


let foo: tuple<u8, i8, u8, i8, u8> = (1, 2, 3, 4, 4, 5, 6)
let foo: (u8) = (6) // says error that not
let foo: u8 = 6
Expand Down Expand Up @@ -179,7 +237,7 @@ f64 > 64 bytes
let a:u32 = 10;
a.add(1)

in cpp
in cpp

std::uint_32 add (std::uint_32 a, std::uint_32 b) {
return a + b;
Expand Down Expand Up @@ -208,13 +266,18 @@ module idk_something_else {
let inst = Foo();

inst.add();


try { errorable_fn_returns_result() } catch (err:Err) { };
}








// ffi "py" import os; ffi "py" import time; ffi "py" import
// math; fn main() { let A: float = 0.0; let B:
// float = 0.0; while true { let z: list<float> = [0.0]*1760; let
Expand Down Expand Up @@ -250,5 +313,3 @@ import "get_fooled/foo.hlix" as foo;

pub import get_fooled::foo::{SomenamespaceInFOO} as thing;



20 changes: 10 additions & 10 deletions language/helix.ebnf
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* Main Program Structure */
Program ::= (ImportStatement | PreprocessorDirective | Declaration | FunctionDefinition | Statement)*
Program ::= (ImportState | PreprocessorDirective | Declaration | FunctionDefinition | Statement)*

/* Modules and Imports */

Module ::= 'module' QualifiedNamespaceID Suite // module ... { :: ... }..

ImportStatement ::= 'import' QualifiedNamespaceID ('::' '{' (QualifiedNamespaceID (',' QualifiedNamespaceID)*) '}')? ('as' QualifiedNamespaceID)? ';'
ImportState ::= 'import' QualifiedNamespaceID ('::' '{' (QualifiedNamespaceID (',' QualifiedNamespaceID)*) '}')? ('as' QualifiedNamespaceID)? ';'

FFIImportStatement ::= 'ffi' String ImportStatement
FFIImportState ::= 'ffi' String ImportState

/* Preprocessor Directives */

Expand All @@ -29,17 +29,17 @@ PyStyleForLoop ::= AnySeparatedID (':' Type)? 'in' Expression

WhileLoop ::= 'while' Expression Suite

IfStatement ::= ('if' | 'unless') Expression Suite
IfState ::= ('if' | 'unless') Expression Suite

ElseIfStatement ::= 'else' ('if' | 'unless') Expression Suite
ElseIfState ::= 'else' ('if' | 'unless') Expression Suite

ElseStatement ::= 'else' Suite
ElseState ::= 'else' Suite

ContinueStatement ::= 'continue' ';'
ContinueState ::= 'continue' ';'

BreakStatement ::= 'break' ';'
BreakState ::= 'break' ';'

SwitchStatement ::= 'switch' '{' (CaseStatement | DefaultCaseStatement)* '}'
SwitchState ::= 'switch' '{' (CaseStatement | DefaultCaseStatement)* '}'

CaseStatement ::= 'case' Expression Suite

Expand Down Expand Up @@ -181,7 +181,7 @@ Assignment ::= AnySeparatedID '=' Expression ';'

Statement ::= VariableDeclaration | Assignment | Expression ';' | ControlFlowStatement | ReturnExpression | YieldExpression | BlockStatement

ControlFlowStatement ::= ForLoop | WhileLoop | IfStatement | UnlessStatement | ElseIfStatement | ElseStatement | ContinueStatement | BreakStatement | SwitchStatement | MatchExpression
ControlFlowStatement ::= ForLoop | WhileLoop | IfState | UnlessStatement | ElseIfState | ElseState | ContinueState | BreakState | SwitchState | MatchExpression

BlockStatement ::= Suite

Expand Down
113 changes: 113 additions & 0 deletions language/helix/pkgs/std/linked_list.hlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//===----------------------------------------- Helix ------------------------------------------===//
// //
// Part of the Helix Project, under the Attribution 4.0 International license (CC BY 4.0). //
// You are allowed to use, modify, redistribute, and create derivative works, even for commercial //
// purposes, provided that you give appropriate credit, and indicate if changes were made. //
// For more information, please visit: https://creativecommons.org/licenses/by/4.0/ //
// //
// SPDX-License-Identifier: CC-BY-4.0 //
// Copyright (c) 2024 (CC BY 4.0) //
// //
//===------------------------------------------------------------------------------------------===//

#[link-time]
import types::{Allocator};
import alloc::{DefaultAllocator};

class IntNode {
let data: int;
let link: *IntNode? = &null;

fn IntNode(self, data: int, link: *IntNode?) {
self.data = data;
self.link = link;
}

fn getData(self) -> int {
return self.data;
}

fn getLink(self) -> *IntNode? {
return self.link;
}

fn setData(self, new_data: int) {
self.data = new_data;
}

fn setLink(self, new_link: *IntNode?) {
self.link = new_link;
}

fn addNodeAfter(self, new_data: int) -> *IntNode {
let link = IntNode(new_data, self.link);
self.link = &link;
return &link;
}
}

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

while (*current).getLink() != &null {
count += 1;
current = current.getLink();
}

return count;
}

fn listSearch(head: *IntNode, target: int) -> *IntNode {
let *current = head;

while (*current).getLink() != &null {
if (*current).getData() == target {
return current;
}
current = current.getLink();
}

return &null;
}

fn listRemoveNode(head: *IntNode, target: int) {
// remove and free

let *current = head;
let *previous = &null;

finally:
try:
// free memory after scope or error
Allocator::free(current);
catch:
// a SegmentationFaultError is diffrent from a SIGSEGV
// it is a safe way to handle segfaults as software errors
// and not hardware errors like SIGSEGV.
panic SegmentationFaultError("Failed to free memory");

while (*current).getLink() != &null {
if (*current).getData() == target {
(*previous).setLink((*current).getLink());
return;
}

previous = current;
current = current.getLink();
}
}

fn main() {
let head = IntNode(1, &null);
let *tail = &head;

for i in 2..100 {
tail = tail.addNodeAfter(i);
}

let length: int = listLength(&head);
let target: *IntNode? = listSearch(&head, 50);
}


Loading

0 comments on commit 919e14d

Please sign in to comment.