Skip to content

Commit

Permalink
fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Dec 2, 2023
1 parent f23727f commit 1cffcd5
Show file tree
Hide file tree
Showing 10 changed files with 7,022 additions and 226 deletions.
3,309 changes: 3,308 additions & 1 deletion model/a.ast-model

Large diffs are not rendered by default.

3,832 changes: 3,624 additions & 208 deletions model/array.ast-model

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions scripting/absytree_internal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ proc editor_model_toggleUseDefaultCellBuilder_void_ModelDocumentEditor_impl*(
proc editor_model_showCompletions_void_ModelDocumentEditor_impl*(
self: ModelDocumentEditor) =
discard
proc editor_model_showCompletionWindow_void_ModelDocumentEditor_impl*(
self: ModelDocumentEditor) =
discard
proc editor_model_hideCompletions_void_ModelDocumentEditor_impl*(
self: ModelDocumentEditor) =
discard
Expand Down
2 changes: 2 additions & 0 deletions scripting/absytree_internal_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ proc editor_model_toggleUseDefaultCellBuilder_void_ModelDocumentEditor_impl(
self: ModelDocumentEditor) {.importc.}
proc editor_model_showCompletions_void_ModelDocumentEditor_impl(
self: ModelDocumentEditor) {.importc.}
proc editor_model_showCompletionWindow_void_ModelDocumentEditor_impl(
self: ModelDocumentEditor) {.importc.}
proc editor_model_hideCompletions_void_ModelDocumentEditor_impl(
self: ModelDocumentEditor) {.importc.}
proc editor_model_selectPrevCompletion_void_ModelDocumentEditor_impl(
Expand Down
2 changes: 2 additions & 0 deletions scripting/editor_model_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ proc toggleUseDefaultCellBuilder*(self: ModelDocumentEditor) =
editor_model_toggleUseDefaultCellBuilder_void_ModelDocumentEditor_impl(self)
proc showCompletions*(self: ModelDocumentEditor) =
editor_model_showCompletions_void_ModelDocumentEditor_impl(self)
proc showCompletionWindow*(self: ModelDocumentEditor) =
editor_model_showCompletionWindow_void_ModelDocumentEditor_impl(self)
proc hideCompletions*(self: ModelDocumentEditor) =
editor_model_hideCompletions_void_ModelDocumentEditor_impl(self)
proc selectPrevCompletion*(self: ModelDocumentEditor) =
Expand Down
14 changes: 14 additions & 0 deletions scripting/editor_model_api_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,20 @@ proc showCompletions*(self: ModelDocumentEditor) =
argsJsonString.cstring)


proc editor_model_showCompletionWindow_void_ModelDocumentEditor_wasm(
arg: cstring): cstring {.importc.}
proc showCompletionWindow*(self: ModelDocumentEditor) =
var argsJson = newJArray()
argsJson.add block:
when ModelDocumentEditor is JsonNode:
self
else:
self.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_model_showCompletionWindow_void_ModelDocumentEditor_wasm(
argsJsonString.cstring)


proc editor_model_hideCompletions_void_ModelDocumentEditor_wasm(arg: cstring): cstring {.
importc.}
proc hideCompletions*(self: ModelDocumentEditor) =
Expand Down
18 changes: 10 additions & 8 deletions src/ast/base_language.nim
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,7 @@ valueComputers[pointerTypeClass.id] = proc(ctx: ModelComputationContextBase, nod
if ctx.getValue(targetTypeNode).isNotNil(targetType):
typ.setChild(IdPointerTypeTarget, targetType)

typ.model = node.model
typ.forEach2 n:
n.model = node.model
node.model.addTempNode(typ)
return typ

return node
Expand Down Expand Up @@ -879,8 +877,14 @@ typeComputers[nodeListClass.id] = proc(ctx: ModelComputationContextBase, node: A
typeComputers[blockClass.id] = proc(ctx: ModelComputationContextBase, node: AstNode): AstNode =
# debugf"compute type for block {node}"

if node.lastChild(IdBlockChildren).getSome(childNode):
return ctx.computeType(childNode)
# todo: maybe find better way to ignore empty line nodes
var lastChild: AstNode = nil
for _, child in node.children(IdBlockChildren):
if child.class != IdEmptyLine:
lastChild = child

if lastChild.isNotNil:
return ctx.computeType(lastChild)

return voidTypeInstance

Expand Down Expand Up @@ -1408,9 +1412,7 @@ typeComputers[allocateClass.id] = proc(ctx: ModelComputationContextBase, node: A
let targetType = ctx.getValue(typeNode)
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 Down
39 changes: 35 additions & 4 deletions src/ast/base_language_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ logCategory "base-language-wasm"

proc genNodeBlock(self: BaseLanguageWasmCompiler, node: AstNode, dest: Destination) =
let typ = self.ctx.computeType(node)
let tempIdx = if dest.kind == Memory:
let wasmValueType = self.toWasmValueType(typ)
let (size, _, _) = self.getTypeAttributes(typ)

# debugf"genNodeBlock: {node}, {dest}, {typ}, {wasmValueType}, {size}"

let tempIdx = if dest.kind == Memory and size > 0: # store result pointer in local, and load again in block
let tempIdx = self.getTempLocal(intTypeInstance)
self.instr(LocalSet, localIdx: tempIdx)
tempIdx.some
else:
WasmLocalIdx.none

self.genBlock(WasmBlockType(kind: ValType, typ: self.toWasmValueType(typ))):
self.genBlock(WasmBlockType(kind: ValType, typ: wasmValueType)):
if tempIdx.getSome(tempIdx):
self.instr(LocalGet, localIdx: tempIdx)

Expand Down Expand Up @@ -81,6 +86,16 @@ proc genNodeBinaryNotEqualExpression(self: BaseLanguageWasmCompiler, node: AstNo
self.instr(I32Ne)
self.genStoreDestination(node, dest)

proc genNodeBinaryAndExpression(self: BaseLanguageWasmCompiler, node: AstNode, dest: Destination) =
self.genNodeBinaryExpression(node, Destination(kind: Stack))
self.instr(I32And)
self.genStoreDestination(node, dest)

proc genNodeBinaryOrExpression(self: BaseLanguageWasmCompiler, node: AstNode, dest: Destination) =
self.genNodeBinaryExpression(node, Destination(kind: Stack))
self.instr(I32Or)
self.genStoreDestination(node, dest)

proc genNodeUnaryNegateExpression(self: BaseLanguageWasmCompiler, node: AstNode, dest: Destination) =
self.instr(I32Const, i32Const: 0)
self.genNodeChildren(node, IdUnaryExpressionChild, Destination(kind: Stack))
Expand Down Expand Up @@ -135,9 +150,10 @@ proc genNodeIfExpression(self: BaseLanguageWasmCompiler, node: AstNode, dest: De
self.currentExpr = WasmExpr()

for i, c in elseCase:
if i > 0 and wasmType.isSome: self.genDrop(c)
self.genNode(c, dest)
if wasmType.isNone: self.genDrop(c)
# if wasmType.isNone:
# log lvlError, fmt"drop {typ} -> {wasmType}"
# self.genDrop(c)

for i in countdown(ifStack.high, 0):
let elseCase = self.currentExpr
Expand Down Expand Up @@ -241,6 +257,7 @@ proc genNodeStringGetLength(self: BaseLanguageWasmCompiler, node: AstNode, dest:
self.instr(I64Const, i64Const: 32)
self.instr(I64ShrU)
self.instr(I32WrapI64)
self.genStoreDestination(node, dest)

proc genNodeNodeReference(self: BaseLanguageWasmCompiler, node: AstNode, dest: Destination) =
let id = node.reference(IdNodeReferenceTarget)
Expand Down Expand Up @@ -345,7 +362,16 @@ proc genNodePrintExpression(self: BaseLanguageWasmCompiler, node: AstNode, dest:
elif typ.class == IdPointerType:
self.instr(Call, callFuncIdx: self.printI32)
elif typ.class == IdString:
let tempIdx = self.getTempLocal(stringTypeInstance)
self.instr(LocalTee, localIdx: tempIdx)
self.instr(I32WrapI64) # pointer

# length
self.instr(LocalGet, localIdx: tempIdx)
self.instr(I64Const, i64Const: 32)
self.instr(I64ShrU)
self.instr(I32WrapI64)

self.instr(Call, callFuncIdx: self.printString)
else:
log lvlError, fmt"genNodePrintExpression: Type not implemented: {`$`(typ, true)}"
Expand Down Expand Up @@ -589,6 +615,9 @@ proc genNodeFunctionDefinition(self: BaseLanguageWasmCompiler, node: AstNode, de
if body.len != 1:
return

self.localIndices.clear
self.currentLocals.setLen 0

let returnType = node.firstChild(IdFunctionDefinitionReturnType).mapIt(self.ctx.getValue(it)).get(voidTypeInstance)
let passReturnAsOutParam = self.shouldPassAsOutParamater(returnType)

Expand Down Expand Up @@ -664,6 +693,8 @@ proc addBaseLanguage*(self: BaseLanguageWasmCompiler) =
self.generators[IdGreaterEqual] = genNodeBinaryGreaterEqualExpression
self.generators[IdEqual] = genNodeBinaryEqualExpression
self.generators[IdNotEqual] = genNodeBinaryNotEqualExpression
self.generators[IdAnd] = genNodeBinaryAndExpression
self.generators[IdOr] = genNodeBinaryOrExpression
self.generators[IdNegate] = genNodeUnaryNegateExpression
self.generators[IdNot] = genNodeUnaryNotExpression
self.generators[IdIntegerLiteral] = genNodeIntegerLiteral
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 @@ -92,7 +92,7 @@ proc newBaseLanguageWasmCompiler*(ctx: ModelComputationContextBase): BaseLanguag

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.printString = result.builder.addImport("env", "print_string", result.builder.addType([I32, I32], []))
result.printLine = result.builder.addImport("env", "print_line", result.builder.addType([], []))
result.intToString = result.builder.addImport("env", "intToString", result.builder.addType([I32], [I32]))
result.stackBase = result.builder.addGlobal(I32, mut=true, 0, id="__stack_base")
Expand Down Expand Up @@ -253,6 +253,8 @@ proc compileToBinary*(self: BaseLanguageWasmCompiler, node: AstNode): seq[uint8]

debugf"{self.builder}"

# discard self.builder.validate()

let binary = self.builder.generateBinary()
return binary

Expand Down
25 changes: 21 additions & 4 deletions src/model_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ proc refilterCompletions(self: ModelDocumentEditor)
proc updateCursor*(self: ModelDocumentEditor, cursor: CellCursor): Option[CellCursor]
proc isThickCursor*(self: ModelDocumentEditor): bool
proc getContextWithMode*(self: ModelDocumentEditor, context: string): string
proc showCompletionWindow*(self: ModelDocumentEditor)

proc toCursor*(map: NodeCellMap, cell: Cell, column: int): CellCursor
proc toCursor*(map: NodeCellMap, cell: Cell, start: bool): CellCursor
Expand Down Expand Up @@ -2660,8 +2661,13 @@ proc insertTextAtCursor*(self: ModelDocumentEditor, input: string): bool {.expos
else:
self.refilterCompletions()

if not self.showCompletions and self.completionsLen == 1 and (self.getCompletion(0).alwaysApply or self.getCompletion(0).name == cell.currentText):
self.applySelectedCompletion()
if not self.showCompletions:
if self.completionsLen == 1 and (self.getCompletion(0).alwaysApply or self.getCompletion(0).name == cell.currentText):
self.applySelectedCompletion()
elif self.getCompletion(self.selectedCompletion).name == cell.currentText:
self.applySelectedCompletion()
else:
self.showCompletionWindow()

self.markDirty()
return true
Expand Down Expand Up @@ -2725,6 +2731,15 @@ proc showCompletions*(self: ModelDocumentEditor) {.expose("editor.model").} =
self.showCompletions = true
self.markDirty()

proc showCompletionWindow*(self: ModelDocumentEditor) {.expose("editor.model").} =
if self.showCompletions:
var newCursor = self.selection.last
newCursor.index = 0
self.cursor = newCursor
self.updateCompletions()
self.showCompletions = true
self.markDirty()

proc hideCompletions*(self: ModelDocumentEditor) {.expose("editor.model").} =
self.unfilteredCompletions.setLen 0
self.showCompletions = false
Expand Down Expand Up @@ -2848,8 +2863,10 @@ proc printI32(a: int32) =
proc printChar(a: int32) =
lineBuffer.add $a.Rune

proc printString(a: cstring) =
lineBuffer.add $a
proc printString(a: cstring, len: int32) =
let str = $a
assert len <= a.len
lineBuffer.add str[0..<len]

proc printLine() =
log lvlInfo, lineBuffer
Expand Down

0 comments on commit 1cffcd5

Please sign in to comment.