added generics ast

This commit is contained in:
2026-01-20 17:47:36 -07:00
parent e7eb194b6c
commit 8f6200f393
4 changed files with 142 additions and 25 deletions

View File

@@ -2,6 +2,10 @@ import * as ohm from "ohm-js";
export const boringGrammar = ohm.grammar(String.raw`
Boringlang {
GenericUsage = "[" ListOf<TypeUsage, ","> "]"
GenericParameter = identifier ":" ListOf<TypeUsage, "+"> -- conditions
| identifier
GenericDeclaration = "[" ListOf<GenericParameter, ","> "]"
ReturnStatement = "return" Expression ";"
LetStatement = "let" identifier (":" TypeUsage)? "=" Expression ";"
AssignmentStatement = identifier "=" Expression ";" -- identifier
@@ -17,12 +21,12 @@ export const boringGrammar = ohm.grammar(String.raw`
LiteralString = "\"" (~"\"" any)* "\""
| "'" (~"'" any)* "'"
LiteralStructField = identifier ":" Expression
LiteralStruct = identifier "{" ListOf<LiteralStructField, ","> "}"
LiteralStruct = identifier (GenericUsage)? "{" ListOf<LiteralStructField, ","> "}"
identifier = (letter | "_")+(letter | digit | "_")*
StructGetter = Expression "." identifier
IfExpression = "if" "(" Expression ")" Block ("else" Block)?
Path = Path "::" identifier -- nested
| identifier -- base
Path = Path "::" identifier (GenericUsage)? -- nested
| identifier (GenericUsage)? -- base
PrimaryExpression = LiteralInt
| LiteralFloat
| LiteralBool
@@ -33,9 +37,9 @@ export const boringGrammar = ohm.grammar(String.raw`
| Block
| IfExpression
| PrimaryExpression
MemberExpression = MemberExpression "." identifier -- structGetter
MemberExpression = MemberExpression "." identifier (GenericUsage)? -- structGetter
| StructExpression
CallExpression = CallExpression "." identifier -- structGetter
CallExpression = CallExpression "." identifier (GenericUsage)? -- structGetter
| CallExpression "(" ListOf<Expression, ","> ")" -- functionCall
| MemberExpression "(" ListOf<Expression, ","> ")" -- memberFunctionCall
| MemberExpression
@@ -47,19 +51,19 @@ export const boringGrammar = ohm.grammar(String.raw`
| MultExpression
Expression = AddExpression
Block = "{" Statement* Expression? "}"
NamedTypeUsage = identifier
NamedTypeUsage = identifier (GenericUsage)?
TypeUsage = NamedTypeUsage
| "fn" "(" ListOf<TypeUsage, ","> ")" ":" TypeUsage -- function_tu
FunctionArgument = identifier ":" TypeUsage
FunctionDeclaration = "fn" identifier "(" ListOf<FunctionArgument, ","> ")" ":" TypeUsage
FunctionDeclaration = "fn" identifier (GenericDeclaration)? "(" ListOf<FunctionArgument, ","> ")" ":" TypeUsage
Function = FunctionDeclaration Block
StructTypeField = identifier ":" TypeUsage
StructTypeDeclaration = "type" identifier "struct" "{" ListOf<StructTypeField, ","> "}"
StructTypeDeclaration = "type" identifier (GenericDeclaration)? "struct" "{" ListOf<StructTypeField, ","> "}"
TraitMethod = FunctionDeclaration ";"
TraitTypeDeclaration = "type" identifier "trait" "{" TraitMethod* "}"
TraitTypeDeclaration = "type" identifier (GenericDeclaration)? "trait" "{" TraitMethod* "}"
TypeDeclaration = StructTypeDeclaration
| TraitTypeDeclaration
Impl = "impl" (NamedTypeUsage "for")? NamedTypeUsage "{" Function* "}"
Impl = "impl" (GenericDeclaration)? (NamedTypeUsage "for")? NamedTypeUsage "{" Function* "}"
ModuleItem = Function
| TypeDeclaration
| Impl