Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Jan 18, 2024
1 parent 23b959c commit e524f7e
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 69 deletions.
3 changes: 0 additions & 3 deletions src/tim.nim
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,5 @@ when defined napibuild:
return %*(x)

elif not isMainModule:
import tim/engine/[meta, parser, logging]
import tim/engine/compilers/html

export parser, html, json
export meta except TimEngine
Empty file modified src/tim.nims
100644 → 100755
Empty file.
56 changes: 44 additions & 12 deletions src/tim/engine/ast.nim
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,24 @@ else:

type
NodeType* = enum
ntInvalid
ntUnknown

ntLitInt = "int"
ntLitString = "string"
ntLitFloat = "float"
ntLitBool = "bool"
ntLitArray = "array"
ntLitObject = "object"
ntArrayStorage = "Array"
ntObjectStorage = "Object"
ntLitFunction = "function"

ntVariableDef = "Variable"
ntFunctionDef = "Function"
ntAssignExpr = "Assignment"
ntHtmlElement = "HtmlElement"
ntInfixExpr = "InfixExpression"
ntMathInfixExpr = "MathExpression"
ntCommandStmt = "CommandStatement"
ntIdent = "Identifier"
ntCall = "FunctionCall"
ntDotExpr
ntBracketExpr
ntConditionStmt = "ConditionStatement"
Expand All @@ -45,6 +44,8 @@ type
ntInclude = "Include"

ntJavaScriptSnippet = "JavaScriptSnippet"
ntYamlSnippet = "YAMLSnippet"
ntJsonSnippet = "JsonSnippet"

CommandType* = enum
cmdEcho = "echo"
Expand Down Expand Up @@ -90,7 +91,7 @@ type

HtmlAttributes* = TableRef[string, seq[Node]]
ConditionBranch* = tuple[expr: Node, body: seq[Node]]

FnParam* = tuple[pName: string, pType: NodeType, pImplVal: Node, meta: Meta]
Node* {.acyclic.} = ref object
case nt*: NodeType
of ntHtmlElement:
Expand Down Expand Up @@ -128,20 +129,29 @@ type
fVal*: float
of ntLitBool:
bVal*: bool
of ntArrayStorage:
of ntLitArray:
arrayItems*: seq[Node]
of ntObjectStorage:
of ntLitObject:
objectItems*: OrderedTableRef[string, Node]
of ntCommandStmt:
cmdType*: CommandType
cmdValue*: Node
of ntIdent:
identName*: string
of ntCall:
callIdent*: string
callArgs*: seq[Node]
of ntDotExpr:
storageType*: StorageType
lhs*, rhs*: Node
of ntJavaScriptSnippet:
jsCode*: string
of ntLitFunction:
fnIdent*: string
fnParams*: OrderedTable[string, FnParam]
fnBody*: seq[Node]
fnReturnType*: NodeType
of ntJavaScriptSnippet, ntYamlSnippet,
ntJsonSnippet:
snippetCode*: string
of ntInclude:
includes*: seq[string]
else: discard
Expand Down Expand Up @@ -363,6 +373,7 @@ proc newNode*(nt: static NodeType): Node =
Node(nt: nt)

proc newString*(tk: TokenTuple): Node =
## Create a new string value node
result = newNode(ntLitString, tk)
result.sVal = tk.value

Expand All @@ -371,23 +382,44 @@ proc newInteger*(v: int, tk: TokenTuple): Node =
result.iVal = v

proc newFloat*(v: float, tk: TokenTuple): Node =
## Create a new float value node
result = newNode(ntLitFloat, tk)
result.fVal = v

proc newBool*(v: bool, tk: TokenTuple): Node =
## Create a new bool value Node
result = newNode(ntLitBool, tk)
result.bVal = v

proc newVariable*(varName: string, varValue: Node, meta: Meta): Node =
## Create a new variable definition Node
result = newNode(ntVariableDef)
result.varName = varName
result.varValue = varvalue
result.meta = meta

proc newVariable*(varName: string, varValue: Node, tk: TokenTuple): Node =
## Create a new variable definition Node
result = newNode(ntVariableDef, tk)
result.varName = varName
result.varValue = varvalue

proc newAssignment*(tk: TokenTuple, varValue: Node): Node =
## Create a new assignment Node
result = newNode(ntAssignExpr, tk)
result.asgnIdent = tk.value
result.asgnVal = varValue

proc newFunction*(tk: TokenTuple, ident: string): Node =
## Create a new Function definition Node
result = newNode(ntLitFunction, tk)
result.fnIdent = ident

proc newCall*(tk: TokenTuple): Node =
## Create a new function call Node
result = newNode(ntCall)
result.callIdent = tk.value

proc newInfix*(lhs, rhs: Node, infixOp: InfixOp, tk: TokenTuple): Node =
result = newNode(ntInfixExpr, tk)
result.infixOp = infixOp
Expand Down Expand Up @@ -418,7 +450,7 @@ proc newCondition*(condIfBranch: ConditionBranch, tk: TokenTuple): Node =

proc newArray*(items: seq[Node] = @[]): Node =
## Creates a new `Array` node
result = newNode(ntArrayStorage)
result = newNode(ntLitArray)
result.arrayItems = items

proc toTimNode*(x: JsonNode): Node =
Expand All @@ -436,12 +468,12 @@ proc toTimNode*(x: JsonNode): Node =
result = newNode(ntLitBool)
result.bVal = x.bval
of JObject:
result = newNode(ntObjectStorage)
result = newNode(ntLitObject)
result.objectItems = newOrderedTable[string, Node]()
for k, v in x:
result.objectItems[k] = toTimNode(v)
of JArray:
result = newNode(ntArrayStorage)
result = newNode(ntLitArray)
for v in x:
result.arrayItems.add(toTimNode(v))
else: discard
Expand Down
Loading

0 comments on commit e524f7e

Please sign in to comment.