Skip to content

Commit

Permalink
working on syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Ze7111 committed Jul 20, 2024
1 parent d188a8e commit 5383465
Show file tree
Hide file tree
Showing 12 changed files with 1,748 additions and 49 deletions.
19 changes: 9 additions & 10 deletions helix/mods/dyn_type.hlx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using "c++" {
priv import <any>;
priv import <memory>;
priv import <typeinfo>;
ffi "c++" {
priv import <any>, <memory>, <typeinfo> in cpp;
}

define make_any_ptr(expr, T): std::make_any<std::unique_ptr<T>>(std::make_unique<T>(expr));
define any_cast(expr, T): std::any_cast<std::unique_ptr<T>>(expr);

priv type Any = std::any;
priv type Any = cpp::std::any;

class Dyntype<T> {
class Dyntype derives Object requires <T> {
priv let value: Any;

fn new(value: T) {
Expand All @@ -25,7 +23,8 @@ class Dyntype<T> {
self.value = make_any_ptr!(value, T);
}

fn get<U>(self) -> void* {
fn get(self) -> void*
requires <U> {
return ptr.get();
}

Expand All @@ -37,13 +36,13 @@ class Dyntype<T> {
fn main() {
let a: Dyntype = 42;

println("a = {}", a.get() as i32);
print("a = {}", a.get<i32>());

a = 3.14;

println("a dyn = {}", a.get() as f64);
print("a dyn = {}", a.get<f64>());

a = "Hello, World!";

println("a dyn = {}", a.get() as string);
print("a dyn = {}", a.get<string>());
}
767 changes: 767 additions & 0 deletions index.html

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions rr_view.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

#-f format | -l | -r | -s | -x] [-t timefmt]
FILE="source/parser/ast/helix.ebnf"
COMMAND="java -jar utils/railroad/rr.war -width:1500 -html $FILE"
VIEW_COMMAND="open ./index.html"
LAST_MODIFIED=$(stat -f "%m" "$FILE")

$COMMAND > index.html
$VIEW_COMMAND

while true; do
CURRENT_MODIFIED=$(stat -f "%m" "$FILE")
if [ "$CURRENT_MODIFIED" != "$LAST_MODIFIED" ]; then
$COMMAND > index.html
echo "file changed"
LAST_MODIFIED=$CURRENT_MODIFIED
fi
sleep 1
done
6 changes: 6 additions & 0 deletions source/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ int main(int argc, char **argv) {

// print the preprocessed tokens

if (parsed_args.emit_ast) {
auto ast = std::make_shared<parser::ast::node::Literals>(std::make_shared<TokenList>(tokens));
u64 num_parsed_tokens = ast->parse().value_or(0);
print(ast->to_json());
}

if (parsed_args.emit_tokens) {
// print(tokens.to_json());
print_tokens(tokens);
Expand Down
77 changes: 44 additions & 33 deletions source/parser/ast/helix.ebnf
Original file line number Diff line number Diff line change
@@ -1,77 +1,86 @@
/* Main Program Structure */
Program ::= (ImportStatement | PreprocessorDirective | VariableDeclaration | FunctionDefinition | Statement)*
Program ::= (ImportStatement | PreprocessorDirective | VariableDeclaration | FunctionDefinition | Statement)*

/* Module Importing */

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

FFIImportStatement ::= 'ffi' String ImportStatement

/* Preprocessor Directives */

PreprocessorDirective ::= DefineMacro | ProcMacro

DefineMacro ::= 'define' Identifier ('(' (UntypedParameter (',' UntypedParameter)*)? ')')? Suite ';'
DefineMacro ::= 'define' Identifier ('(' (UntypedParameter (',' UntypedParameter)*)? ')')? Suite ';'

ProcMacro ::= 'macro' Identifier '(' (UntypedParameter (',' UntypedParameter)*)? ')' '->' Type '{' (Statement | Expression) '}' ';'
ProcMacro ::= 'macro' Identifier '(' (TypedParameter (',' TypedParameter)*)? ')' '->' Type '{' (Statement | Expression) '}' ';'

/* Control Flow */

ForLoop ::= 'for' '(' (CStyleForLoop | PyStyleForLoop) ')' | (CStyleForLoop | PyStyleForLoop) Suite
ForLoop ::= 'for' '(' (CStyleForLoop | PyStyleForLoop) ')' | (CStyleForLoop | PyStyleForLoop) Suite

CStyleForLoop ::= (VariableDeclaration | Expression?) ';' Expression? ';' Expression?
CStyleForLoop ::= (VariableDeclaration | Expression?) ';' Expression? ';' Expression?

PyStyleForLoop ::= AnySeparatedID 'in' Expression
PyStyleForLoop ::= AnySeparatedID (':' Type)? 'in' Expression

WhileLoop ::= 'while' Expression Suite
WhileLoop ::= 'while' Expression Suite

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

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

ElseIfStatement ::= 'else' 'if' Expression Suite

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

ContinueStatement ::= 'continue'

BreakStatement ::= 'break'

SwitchStatement ::= 'switch' '{' ((('case' Expression) | 'default') Suite)* '}'

MatchExpression ::= 'match' '{' ((Expression | '_') '->' (CodeBlock | (':' Expression)) (',' (Expression | '_') '->' (CodeBlock | (':' Expression)))*) '}'

/* Functions and Methods */
FunctionDecl ::= 'fn' QualifiedNamespaceID '(' (TypedParameter (',' TypedParameter)*)? (DefaultArgument (',' DefaultArgument)*)? ')' ('->' Type)?

FunctionDefinition ::= AccessModifiers? (('fn' QualifiedNamespaceID '(' (TypedParameter (',' TypedParameter)*)? (DefaultArgument (',' DefaultArgument)*)? ')' ('->' Type)?
| 'inline' ('static' | 'const')? 'fn' QualifiedNamespaceID '(' (TypedParameter (',' TypedParameter)*)? (DefaultArgument (',' DefaultArgument)*)? ')' ('->' Type)?
| 'async' 'fn' QualifiedNamespaceID '(' (TypedParameter (',' TypedParameter)*)? (DefaultArgument (',' DefaultArgument)*)? ')' ('->' Type)?
| 'static' 'fn' QualifiedNamespaceID '(' (TypedParameter (',' TypedParameter)*)? (DefaultArgument (',' DefaultArgument)*)? ')' ('->' Type)?
| 'const' 'fn' QualifiedNamespaceID '(' (TypedParameter (',' TypedParameter)*)? (DefaultArgument (',' DefaultArgument)*)? ')' ('->' Type)?
| 'ffi' String 'fn' QualifiedNamespaceID '(' (TypedParameter (',' TypedParameter)*)? (DefaultArgument (',' DefaultArgument)*)? ')' ('->' Type)?) GenericDeclaration? (Suite | ';'))
FunctionDefinition ::= BaseObjectDecl FunctionDecl GenericDeclaration? (Suite | ';')

OperatorDefinition ::= 'op' Operator 'as' QualifiedNamespaceID '(' (TypedParameter (',' TypedParameter)*)? (DefaultArgument (',' DefaultArgument)*)? ')' ('->' Type)? (Suite | ';')
OperatorDefinition ::= 'op' Operator 'as' FunctionDecl (Suite | ';')

ReturnExpression ::= 'return' (Expression)? ';'

YieldExpression ::= 'yield' (Expression)? ';'

/* Variable Declarations */
/* Class and Struct */

BaseObjectDecl ::= AccessModifiers? (('inline' ('static' | 'const')?) | 'async' | 'static' | 'const' | 'final' | FFIAccess)

ClassDefintion ::= BaseObjectDecl 'class' QualifiedNamespaceID (('derives' | ':') (QualifiedNamespaceID (',' QualifiedNamespaceID)*))? GenericDeclaration? (Suite | ';')

EnumDefintion ::= BaseObjectDecl 'enum' QualifiedNamespaceID (('{' ((TypedParameter | UntypedParameter) ('=' Expression)? (';' (TypedParameter | UntypedParameter) ('=' Expression)?)*) '}') | ';')

VariableDeclaration ::= AccessModifiers? (('let' | 'shared') ((UntypedParameter '=' Expression) | (TypedParameter ('=' Expression)? )) ';'
| 'shared'? 'const' TypedParameter '=' Expression ';')
StructDefintion ::= BaseObjectDecl 'struct' GenericDeclaration? (Suite | ';')

UnionDefintion ::= BaseObjectDecl 'union' GenericDeclaration? (('{' ((TypedParameter | UntypedParameter) (';' (TypedParameter | UntypedParameter))*) '}') | ';')

TypeDefintion ::=

/* Variable Declarations */
VariableDeclaration ::= AccessModifiers? 'let' (
(('unsfae')? ('final' | 'atomic' | 'shared')? (TypedParameter | UntypedParameter) ('=' Expression)?)
| ('const' (TypedParameter) '=' Expression)
) ';'

/* Type Definitions */

GenericAccess ::= '<' (QualifiedNamespaceID (',' QualifiedNamespaceID)*)? '>'
GenericAccess ::= '<' ((QualifiedNamespaceID | Literal) (',' (QualifiedNamespaceID | Literal))*)? '>'

TypeBound ::= UntypedParameter is (QualifiedNamespaceID GenericAccess? ('&' QualifiedNamespaceID GenericAccess?)*)

GenericDeclaration ::= 'requires' ((TypeBound (',' TypeBound)*) | ('const' TypedParameter ('=' Expression)?))
GenericType ::= (('const' TypedParameter) | UntypedParameter) ('=' Expression)?

TypeBound ::= ('<' (UntypedParameter | (UntypedParameter (',' UntypedParameter)*)) '>')
('if' QualifiedNamespaceID GenericAccess? ('+' QualifiedNamespaceID GenericAccess?)*)?
('=' Type)?
GenericDeclaration ::= 'requires' '<' (GenericType (',' GenericType)*)? '>' ('if' TypeBound (',' TypeBound)*)?

TupleType ::= '(' (Type (',' Type)*) ')'

Expand Down Expand Up @@ -113,15 +122,15 @@ ConditionalExpression ::= Expression '?' Expression ':' Expression

/* Statements */

Assignment ::= AnySeparatedID '=' Expression ';'
Assignment ::= AnySeparatedID '=' Expression ';'

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

ControlFlowStatement ::= ForLoop | WhileLoop | IfStatement | UnlessStatement | ElseIfStatement | ElseStatement | ContinueStatement | BreakStatement | SwitchStatement | MatchExpression

BlockStatement ::= Suite
BlockStatement ::= Suite

Statements ::= Statement*
Statements ::= Statement*

/* Code Structure */

Expand All @@ -131,6 +140,8 @@ CodeLine ::= ':' (Statement | Expression) ';'

Suite ::= CodeLine | CodeBlock

FFIAccess ::= 'ffi' String

TypedParameter ::= Identifier ':' Type

UntypedParameter ::= Identifier
Expand Down
6 changes: 3 additions & 3 deletions source/parser/ast/keywords.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
| **N** | **Access Specifiers** | **Module Importing** | **Concurrency** | **Other** |
|-------|-----------------------|----------------------|-----------------|-----------|
| 1 | module | as | atomic | delete |
| 2 | private | ffi | await | is |
| 3 | public | import | spawn | null |
| 4 | protected | | thread | nullptr |
| 2 | priv | ffi | await | is |
| 3 | pub | import | spawn | null |
| 4 | prot | | thread | nullptr |
| 5 | | | | unsafe |

| **N** | **Complex Types** | **Signed Types** | **Unsigned Types** | **Data Types** | **Float Types** |
Expand Down
2 changes: 0 additions & 2 deletions source/parser/cst/include/cst.hh
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ struct CSTBase<void> {
virtual ~CSTBase() = default;

[[nodiscard]] virtual ParseResult parse() = 0;
[[nodiscard]] virtual bool peek() const = 0;
[[nodiscard]] virtual std::string to_json(u32 depth = 0) const = 0;
[[nodiscard]] inline virtual bool HAS_HIGH_PEEK_OVERHEAD() = 0;
};

template <typename T>
Expand Down
42 changes: 41 additions & 1 deletion tests/main.hlx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test_imports::test_imports;

fn main() {
fn main()
requires <const T: string> {
let a = "bruh";
print(
if a == "bruh":
Expand All @@ -13,6 +14,8 @@ fn main() {
);
}

class ASum


module bar {
module foo {
Expand All @@ -28,6 +31,43 @@ module bar {
}


// import std::range;
// import std::file_sytem;

// const cols: int = 14;

// fn parse_char(c: char) -> string {
// return f"{c:02X}"
// }

// fn main() {
// let path: string = file_sytem::get_bin_path();

// file_sytem::open(path, "rb") as file {
// // read 14 cols in chunks of 20 chars
// for chunk: string in range(file.read(cols), 20) {
// let hex_chunk: string;

// for character: char in chunk {
// // types for range based loops can be inferd but specifing
// // it would cast the iter output to the specified type
// // so `for character in chunk` would just work.

// if character < 0x20 || character > 0x7E {
// hex_chunk += ".";
// } else {
// hex_chunk += character as string;
// }
// }

// let hex_str: string = chunk.map(parse_char).join(" ");
// let padding: string = " " * (3 * (cols - chunk.len()));

// file.write(f"{hex_str}{padding} {hex_chunk}\n");
// }
// }
// }

/*
bar/
foo.hlx
Expand Down
28 changes: 28 additions & 0 deletions utils/railroad/LICENSE/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
RR - Railroad Diagram Generator

Copyright 2010-2024 Gunther Rademacher <[email protected]>

Licensed under the Apache 2.0 License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.

This distribution also includes

- Saxon-HE from Saxonica.com
- TagSoup
- Apache Batik
- Apache XML Graphics Commons
- Apache XML Commons XML APIs
- Ace Editor

For their license information see THIRD-PARTY-NOTICES.txt.

Thank you for choosing RR - Railroad Diagram Generator.
Loading

0 comments on commit 5383465

Please sign in to comment.