Skip to content

Commit

Permalink
fixed some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Dec 1, 2023
1 parent d5fdff3 commit 7d90759
Show file tree
Hide file tree
Showing 10 changed files with 2,057 additions and 32 deletions.
1,312 changes: 1,311 additions & 1 deletion model/a.ast-model

Large diffs are not rendered by default.

611 changes: 609 additions & 2 deletions model/array.ast-model

Large diffs are not rendered by default.

59 changes: 55 additions & 4 deletions src/ast/base_language.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let metaTypeClass* = newNodeClass(IdType, "Type", alias="type", base=expressionC
let stringTypeClass* = newNodeClass(IdString, "StringType", alias="string", base=expressionClass)
let intTypeClass* = newNodeClass(IdInt, "IntType", alias="int", base=expressionClass)
let voidTypeClass* = newNodeClass(IdVoid, "VoidType", alias="void", base=expressionClass)
let charTypeClass * = newNodeClass(IdChar, "CharType", alias="char", base=expressionClass)
let functionTypeClass* = newNodeClass(IdFunctionType, "FunctionType", base=expressionClass,
children=[
NodeChildDescription(id: IdFunctionTypeParameterTypes, role: "parameterTypes", class: expressionClass.id, count: ChildCount.ZeroOrMore),
Expand Down Expand Up @@ -82,6 +83,14 @@ let derefClass* = newNodeClass(IdDeref, "Deref", alias="deref", base=expressionC
children=[
NodeChildDescription(id: IdDerefValue, role: "value", class: expressionClass.id, count: ChildCount.One)])

let stringGetPointerClass* = newNodeClass(IdStringGetPointer, "StringGetPointer", base=expressionClass,
children=[
NodeChildDescription(id: IdStringGetPointerValue, role: "value", class: expressionClass.id, count: ChildCount.One)])

let stringGetLengthClass* = newNodeClass(IdStringGetLength, "StringGetLength", base=expressionClass,
children=[
NodeChildDescription(id: IdStringGetLengthValue, role: "value", class: expressionClass.id, count: ChildCount.One)])

let arrayAccessClass* = newNodeClass(IdArrayAccess, "ArrayAccess", alias="[]", base=expressionClass,
children=[
NodeChildDescription(id: IdArrayAccessValue, role: "value", class: expressionClass.id, count: ChildCount.One),
Expand Down Expand Up @@ -427,6 +436,22 @@ builder.addBuilderFor derefClass.id, idNone(), proc(builder: CellBuilder, node:
cell.add builder.buildChildren(map, node, IdDerefValue, &{LayoutHorizontal})
return cell

builder.addBuilderFor stringGetPointerClass.id, idNone(), proc(builder: CellBuilder, node: AstNode): Cell =
var cell = CollectionCell(id: newId().CellId, node: node, uiFlags: &{LayoutHorizontal})
cell.fillChildren = proc(map: NodeCellMap) =
cell.add builder.buildChildren(map, node, IdStringGetPointerValue, &{LayoutHorizontal})
cell.add ConstantCell(node: node, text: ".", flags: &{NoSpaceLeft, NoSpaceRight}, themeForegroundColors: @["punctuation"], disableEditing: true)
cell.add ConstantCell(node: node, text: "ptr", themeForegroundColors: @["keyword"], disableEditing: true)
return cell

builder.addBuilderFor stringGetLengthClass.id, idNone(), proc(builder: CellBuilder, node: AstNode): Cell =
var cell = CollectionCell(id: newId().CellId, node: node, uiFlags: &{LayoutHorizontal})
cell.fillChildren = proc(map: NodeCellMap) =
cell.add builder.buildChildren(map, node, IdStringGetLengthValue, &{LayoutHorizontal})
cell.add ConstantCell(node: node, text: ".", flags: &{NoSpaceLeft, NoSpaceRight}, themeForegroundColors: @["punctuation"], disableEditing: true)
cell.add ConstantCell(node: node, text: "len", themeForegroundColors: @["keyword"], disableEditing: true)
return cell

builder.addBuilderFor IdArrayAccess, idNone(), proc(builder: CellBuilder, node: AstNode): Cell =
var cell = CollectionCell(id: newId().CellId, node: node, uiFlags: &{LayoutHorizontal})
cell.fillChildren = proc(map: NodeCellMap) =
Expand Down Expand Up @@ -582,6 +607,10 @@ builder.addBuilderFor intTypeClass.id, idNone(), proc(builder: CellBuilder, node
var cell = AliasCell(id: newId().CellId, node: node, themeForegroundColors: @["storage.type", "&editor.foreground"], disableEditing: true)
return cell

builder.addBuilderFor charTypeClass.id, idNone(), proc(builder: CellBuilder, node: AstNode): Cell =
var cell = AliasCell(id: newId().CellId, node: node, themeForegroundColors: @["storage.type", "&editor.foreground"], disableEditing: true)
return cell

builder.addBuilderFor printExpressionClass.id, idNone(), proc(builder: CellBuilder, node: AstNode): Cell =
var cell = CollectionCell(id: newId().CellId, node: node, uiFlags: &{LayoutHorizontal})
cell.fillChildren = proc(map: NodeCellMap) =
Expand Down Expand Up @@ -622,6 +651,7 @@ let metaTypeInstance* = newAstNode(metaTypeClass)
let stringTypeInstance* = newAstNode(stringTypeClass)
let intTypeInstance* = newAstNode(intTypeClass)
let voidTypeInstance* = newAstNode(voidTypeClass)
let charTypeInstance* = newAstNode(charTypeClass)

typeComputers[metaTypeClass.id] = proc(ctx: ModelComputationContextBase, node: AstNode): AstNode =
# debugf"compute type for meta type literal {node}"
Expand All @@ -632,6 +662,9 @@ typeComputers[stringTypeClass.id] = proc(ctx: ModelComputationContextBase, node:
typeComputers[intTypeClass.id] = proc(ctx: ModelComputationContextBase, node: AstNode): AstNode =
# debugf"compute type for int type literal {node}"
return metaTypeInstance
typeComputers[charTypeClass.id] = proc(ctx: ModelComputationContextBase, node: AstNode): AstNode =
# debugf"compute type for char type literal {node}"
return metaTypeInstance
typeComputers[voidTypeClass.id] = proc(ctx: ModelComputationContextBase, node: AstNode): AstNode =
# debugf"compute type for void type literal {node}"
return metaTypeInstance
Expand All @@ -648,6 +681,9 @@ valueComputers[stringTypeClass.id] = proc(ctx: ModelComputationContextBase, node
valueComputers[intTypeClass.id] = proc(ctx: ModelComputationContextBase, node: AstNode): AstNode =
# debugf"compute value for int type literal {node}"
return intTypeInstance
valueComputers[charTypeClass.id] = proc(ctx: ModelComputationContextBase, node: AstNode): AstNode =
# debugf"compute value for char type literal {node}"
return charTypeInstance
valueComputers[voidTypeClass.id] = proc(ctx: ModelComputationContextBase, node: AstNode): AstNode =
# debugf"compute value for void type literal {node}"
return voidTypeInstance
Expand Down Expand Up @@ -1386,9 +1422,7 @@ typeComputers[addressOfClass.id] = proc(ctx: ModelComputationContextBase, node:
let targetType = ctx.computeType(valueNode)
var typ = newAstNode(pointerTypeClass)
typ.add(IdPointerTypeTarget, targetType)
typ.model = node.model
typ.forEach2 n:
n.model = node.model
node.model.addTempNode(typ)
return typ

return voidTypeInstance
Expand All @@ -1403,6 +1437,17 @@ typeComputers[derefClass.id] = proc(ctx: ModelComputationContextBase, node: AstN

return voidTypeInstance

typeComputers[stringGetPointerClass.id] = proc(ctx: ModelComputationContextBase, node: AstNode): AstNode =
# debugf"compute type for deref {node}"
var typ = newAstNode(pointerTypeClass)
typ.add(IdPointerTypeTarget, charTypeInstance)
node.model.addTempNode(typ)
return typ

typeComputers[stringGetLengthClass.id] = proc(ctx: ModelComputationContextBase, node: AstNode): AstNode =
# debugf"compute type for deref {node}"
return intTypeInstance

typeComputers[arrayAccessClass.id] = proc(ctx: ModelComputationContextBase, node: AstNode): AstNode =
# debugf"compute type for deref {node}"

Expand Down Expand Up @@ -1512,11 +1557,15 @@ scopeComputers[IdParameterDecl] = proc(ctx: ModelComputationContextBase, node: A
# debugf"compute scope for parameter {node}"
return ctx.computeDefaultScope(node)

scopeComputers[IdThenCase] = proc(ctx: ModelComputationContextBase, node: AstNode): seq[AstNode] =
# debugf"compute scope for then case {node}"
return ctx.computeDefaultScope(node)

let baseLanguage* = newLanguage(IdBaseLanguage, @[
namedInterface, declarationInterface,

# typeClass,
metaTypeClass, stringTypeClass, intTypeClass, voidTypeClass, functionTypeClass, structTypeClass, pointerTypeClass,
metaTypeClass, stringTypeClass, intTypeClass, charTypeClass, voidTypeClass, functionTypeClass, structTypeClass, pointerTypeClass,

expressionClass, binaryExpressionClass, unaryExpressionClass, emptyLineClass,
numberLiteralClass, stringLiteralClass, boolLiteralClass, nodeReferenceClass, emptyClass, genericTypeClass, constDeclClass, letDeclClass, varDeclClass, nodeListClass, blockClass, callClass, thenCaseClass, ifClass, whileClass,
Expand All @@ -1526,6 +1575,7 @@ let baseLanguage* = newLanguage(IdBaseLanguage, @[
lessExpressionClass, lessEqualExpressionClass, greaterExpressionClass, greaterEqualExpressionClass, equalExpressionClass, notEqualExpressionClass, andExpressionClass, orExpressionClass, orderExpressionClass,
negateExpressionClass, notExpressionClass,
appendStringExpressionClass, printExpressionClass, buildExpressionClass, allocateClass,
stringGetPointerClass, stringGetLengthClass,

structDefinitionClass, structMemberDefinitionClass, structParameterClass, structMemberAccessClass,
addressOfClass, derefClass, arrayAccessClass,
Expand All @@ -1537,6 +1587,7 @@ let baseModel* = block:
model.addRootNode(intTypeInstance)
model.addRootNode(stringTypeInstance)
model.addRootNode(voidTypeInstance)
model.addRootNode(charTypeInstance)
model.addRootNode(metaTypeInstance)

model
Expand Down
33 changes: 29 additions & 4 deletions src/ast/base_language_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ proc genNodeLetDecl(self: BaseLanguageWasmCompiler, node: AstNode, dest: Destina

if node.firstChild(IdLetDeclValue).getSome(value):
self.instr(LocalGet, localIdx: self.currentBasePointer)
self.genNode(value, Destination(kind: Memory, offset: offset.uint32, align: 0))
self.instr(I32Const, i32Const: offset.int32)
self.instr(I32Add)
self.genNode(value, Destination(kind: Memory, offset: 0, align: 0))

# self.instr(LocalTee, localIdx: index)

Expand All @@ -196,7 +198,9 @@ proc genNodeVarDecl(self: BaseLanguageWasmCompiler, node: AstNode, dest: Destina

if node.firstChild(IdVarDeclValue).getSome(value):
self.instr(LocalGet, localIdx: self.currentBasePointer)
self.genNode(value, Destination(kind: Memory, offset: offset.uint32, align: 0))
self.instr(I32Const, i32Const: offset.int32)
self.instr(I32Add)
self.genNode(value, Destination(kind: Memory, offset: 0, align: 0))

# self.instr(LocalTee, localIdx: index)

Expand Down Expand Up @@ -227,6 +231,17 @@ proc genNodeContinueExpression(self: BaseLanguageWasmCompiler, node: AstNode, de
proc genNodeReturnExpression(self: BaseLanguageWasmCompiler, node: AstNode, dest: Destination) =
discard

proc genNodeStringGetPointer(self: BaseLanguageWasmCompiler, node: AstNode, dest: Destination) =
self.genNodeChildren(node, IdStringGetPointerValue, Destination(kind: Stack))
self.instr(I32WrapI64)
self.genStoreDestination(node, dest)

proc genNodeStringGetLength(self: BaseLanguageWasmCompiler, node: AstNode, dest: Destination) =
self.genNodeChildren(node, IdStringGetLengthValue, Destination(kind: Stack))
self.instr(I64Const, i64Const: 32)
self.instr(I64ShrU)
self.instr(I32WrapI64)

proc genNodeNodeReference(self: BaseLanguageWasmCompiler, node: AstNode, dest: Destination) =
let id = node.reference(IdNodeReferenceTarget)
if node.resolveReference(IdNodeReferenceTarget).getSome(target) and target.class == IdConstDecl:
Expand Down Expand Up @@ -300,7 +315,9 @@ proc genAssignmentExpression(self: BaseLanguageWasmCompiler, node: AstNode, dest
discard
of Stack(stackOffset: @offset):
self.instr(LocalGet, localIdx: self.currentBasePointer)
valueDest = Destination(kind: Memory, offset: offset.uint32, align: 0)
self.instr(I32Const, i32Const: offset.int32)
self.instr(I32Add)
valueDest = Destination(kind: Memory, offset: 0, align: 0)
else:
self.genNode(targetNode, Destination(kind: LValue))
valueDest = Destination(kind: Memory, offset: 0, align: 0)
Expand All @@ -323,6 +340,8 @@ proc genNodePrintExpression(self: BaseLanguageWasmCompiler, node: AstNode, dest:
let typ = self.ctx.computeType(c)
if typ.class == IdInt:
self.instr(Call, callFuncIdx: self.printI32)
elif typ.class == IdChar:
self.instr(Call, callFuncIdx: self.printChar)
elif typ.class == IdPointerType:
self.instr(Call, callFuncIdx: self.printI32)
elif typ.class == IdString:
Expand Down Expand Up @@ -467,7 +486,7 @@ proc genNodeStructMemberAccessExpression(self: BaseLanguageWasmCompiler, node: A
self.loadInstr(instr, offset.uint32, 0)

of Memory():
if offset > 0:
if offset > 0: # todo: is this offset correct? Do we need that/is adding it after generating the value correct?
self.instr(I32Const, i32Const: offset.int32)
self.instr(I32Add)
self.instr(I32Const, i32Const: size)
Expand Down Expand Up @@ -612,6 +631,8 @@ proc genNodeFunctionDefinition(self: BaseLanguageWasmCompiler, node: AstNode, de

# epilogue
self.instr(LocalGet, localIdx: self.currentBasePointer)
self.instr(I32Const, i32Const: requiredStackSize)
self.instr(I32Add)
self.instr(GlobalSet, globalIdx: self.stackPointer)

proc getFunctionInputOutput(self: BaseLanguageWasmCompiler, node: AstNode): tuple[inputs: seq[WasmValueType], outputs: seq[WasmValueType]] =
Expand Down Expand Up @@ -666,13 +687,17 @@ proc addBaseLanguage*(self: BaseLanguageWasmCompiler) =
self.generators[IdArrayAccess] = genNodeArrayAccess
self.generators[IdAllocate] = genNodeAllocate
self.generators[IdReturnExpression] = genNodeReturnExpression
self.generators[IdStringGetPointer] = genNodeStringGetPointer
self.generators[IdStringGetLength] = genNodeStringGetLength

self.wasmValueTypes[IdInt] = (WasmValueType.I32, I32Load, I32Store) # int32
self.wasmValueTypes[IdChar] = (WasmValueType.I32, I32Load8U, I32Store8) # int32
self.wasmValueTypes[IdPointerType] = (WasmValueType.I32, I32Load, I32Store) # pointer
self.wasmValueTypes[IdString] = (WasmValueType.I64, I64Load, I64Store) # (len << 32) | ptr
self.wasmValueTypes[IdFunctionType] = (WasmValueType.I32, I32Load, I32Store) # table index

self.typeAttributes[IdInt] = (4'i32, 4'i32, false)
self.typeAttributes[IdChar] = (1'i32, 1'i32, false)
self.typeAttributes[IdPointerType] = (4'i32, 4'i32, false)
self.typeAttributes[IdString] = (8'i32, 4'i32, false)
self.typeAttributes[IdFunctionType] = (0'i32, 1'i32, false)
Expand Down
6 changes: 4 additions & 2 deletions src/ast/cells.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ method setText*(cell: Cell, text: string, slice: Slice[int] = 0..0) {.base.} = d

proc currentText*(cell: Cell): string =
# todo: maybe remove display text?
# if not cell.displayText.isNone:
# return cell.displayText.get
if not cell.displayText.isNone:
return cell.displayText.get
return cell.getText

proc `currentText=`*(cell: Cell, text: string) =
Expand Down Expand Up @@ -215,6 +215,8 @@ method setText*(cell: PropertyCell, text: string, slice: Slice[int] = 0..0) =
let boolValue = text.parseBool
cell.targetNode.setProperty(cell.property, PropertyValue(kind: PropertyType.Bool, boolValue: boolValue), slice)

cell.displayText = string.none

except CatchableError:
discard

Expand Down
4 changes: 3 additions & 1 deletion src/ast/generator_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type
case kind*: DestinationStorage
of Stack: discard
of Memory:
offset*: uint32
offset: uint32 # todo: remove?
align*: uint32
of Discard: discard
of LValue: discard
Expand Down Expand Up @@ -53,6 +53,7 @@ type

# imported
printI32: WasmFuncIdx
printChar: WasmFuncIdx
printString: WasmFuncIdx
printLine: WasmFuncIdx
intToString: WasmFuncIdx
Expand Down Expand Up @@ -90,6 +91,7 @@ proc newBaseLanguageWasmCompiler*(ctx: ModelComputationContextBase): BaseLanguag
result.builder.addExport("memory", 0.WasmMemIdx)

result.printI32 = result.builder.addImport("env", "print_i32", result.builder.addType([I32], []))
result.printChar = result.builder.addImport("env", "print_char", result.builder.addType([I32], []))
result.printString = result.builder.addImport("env", "print_string", result.builder.addType([I32], []))
result.printLine = result.builder.addImport("env", "print_line", result.builder.addType([], []))
result.intToString = result.builder.addImport("env", "intToString", result.builder.addType([I32], [I32]))
Expand Down
8 changes: 4 additions & 4 deletions src/ast/model.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[options, strutils, hashes, tables, strformat, sugar, sequtils]
import std/[options, strutils, hashes, tables, strformat, sugar, sequtils, sets]
import fusion/matching
import chroma
import workspaces/[workspace]
Expand Down Expand Up @@ -183,7 +183,7 @@ type
tempNodes {.getter.}: seq[AstNode]
languages {.getter.}: seq[Language]
models {.getter.}: seq[Model]
importedModels {.getter.}: seq[ModelId]
importedModels {.getter.}: HashSet[ModelId]
classesToLanguages {.getter.}: Table[ClassId, Language]
childClasses {.getter.}: Table[ClassId, seq[NodeClass]]
nodes {.getter.}: Table[NodeId, AstNode]
Expand Down Expand Up @@ -349,7 +349,7 @@ proc hasLanguage*(self: Model, language: LanguageId): bool =

proc addImport*(self: Model, model: Model) =
# log lvlWarn, fmt"addImport to {self.path} ({self.id}): {model.path} ({model.id})"
self.importedModels.add model.id
self.importedModels.incl model.id
self.models.add model

proc addLanguage*(self: Model, language: Language) =
Expand Down Expand Up @@ -1228,7 +1228,7 @@ proc loadFromJson*(project: Project, model: Model, workspace: WorkspaceFolder, p
if json.hasKey("models"):
for modelIdJson in json["models"]:
let id = modelIdJson.jsonTo ModelId
model.importedModels.add id
model.importedModels.incl id
if project.resolveModel(workspace, id).await.getSome(m):
model.addImport(m)
else:
Expand Down
12 changes: 7 additions & 5 deletions src/ast_ids.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let IdAppendString* = "62e53397564d29f772934500".parseId.ClassId
let IdInt* = "62e53397564d29f772934501".parseId.ClassId
let IdString* = "62e53397564d29f772934502".parseId.ClassId
let IdVoid* = "62e53397564d29f772934503".parseId.ClassId
let IdChar* = "654fbb281446e19b38225262".parseId.ClassId
let IdLess* = "62e53398564d29f772934504".parseId.ClassId
let IdLessEqual* = "62e53398564d29f772934505".parseId.ClassId
let IdGreater* = "62e53398564d29f772934506".parseId.ClassId
Expand Down Expand Up @@ -155,6 +156,12 @@ let IdAllocateCount* = "654fbb281446e19b38225213".parseId.RoleId
let IdGenericType* = "654fbb281446e19b3822521a".parseId.ClassId
let IdGenericTypeValue* = "654fbb281446e19b3822521b".parseId.RoleId

let IdStringGetPointer* = "654fbb281446e19b38225252".parseId.ClassId
let IdStringGetPointerValue* = "654fbb281446e19b38225253".parseId.RoleId

let IdStringGetLength* = "654fbb281446e19b38225254".parseId.ClassId
let IdStringGetLengthValue* = "654fbb281446e19b38225255".parseId.RoleId

# lang language
let IdLangLanguage* = "654fbb281446e19b3822523f".parseId.LanguageId

Expand Down Expand Up @@ -210,10 +217,6 @@ let IdIRoleDescriptor* = "654fbb281446e19b3822524c".parseId.ClassId

# new ids

let Id654fbb281446e19b38225252* = "654fbb281446e19b38225252".parseId
let Id654fbb281446e19b38225253* = "654fbb281446e19b38225253".parseId
let Id654fbb281446e19b38225254* = "654fbb281446e19b38225254".parseId
let Id654fbb281446e19b38225255* = "654fbb281446e19b38225255".parseId
let Id654fbb281446e19b38225256* = "654fbb281446e19b38225256".parseId
let Id654fbb281446e19b38225257* = "654fbb281446e19b38225257".parseId
let Id654fbb281446e19b38225258* = "654fbb281446e19b38225258".parseId
Expand All @@ -227,7 +230,6 @@ let Id654fbb281446e19b3822525f* = "654fbb281446e19b3822525f".parseId
let Id654fbb281446e19b38225260* = "654fbb281446e19b38225260".parseId
let Id654fbb281446e19b38225261* = "654fbb281446e19b38225261".parseId
let Id654fbb281446e19b38225262* = "654fbb281446e19b38225262".parseId
let Id654fbb281446e19b38225263* = "654fbb281446e19b38225263".parseId

# import strformat
# for i in 0..100:
Expand Down
Loading

0 comments on commit 7d90759

Please sign in to comment.