Skip to content

Commit

Permalink
(fmt) format code
Browse files Browse the repository at this point in the history
  • Loading branch information
xTrayambak committed Nov 9, 2024
1 parent 3581e92 commit 63e825e
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 49 deletions.
9 changes: 5 additions & 4 deletions src/bali/grammar/token.nim
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ type
Invalid
Shebang
InvalidShebang

MalformedStringReason* {.pure.} = enum
UnclosedString
BadUnicodeEscape

UnicodeEscapeIntTooBig
UnicodeEscapeIntTooSmall

Expand Down Expand Up @@ -160,10 +159,12 @@ func getError*(token: Token): Option[string] =
of MalformedStringReason.BadUnicodeEscape:
return some("malformed Unicode character escape sequence")
of MalformedStringReason.UnicodeEscapeIntTooBig:
return some("Unicode codepoint must not be greater than 0x10FFFF in escape sequence")
return
some("Unicode codepoint must not be greater than 0x10FFFF in escape sequence")
of MalformedStringReason.UnicodeEscapeIntTooSmall:
return some("Unicode codepoint cannot be less than zero")
else: discard
else:
discard

const Keywords* = {
"const": TokenKind.Const,
Expand Down
43 changes: 27 additions & 16 deletions src/bali/grammar/tokenizer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ proc consumeBackslash*(tokenizer: Tokenizer): Result[char, MalformedStringReason
if tokenizer.eof:
debug "tokenizer: `\\` is abruptly ended by the end of the stream, returning Invalid"
return

case &tokenizer.charAt()
of 'u':
debug "tokenizer: backslash starts unicode sequence"
Expand All @@ -202,7 +202,8 @@ proc consumeBackslash*(tokenizer: Tokenizer): Result[char, MalformedStringReason
if not tokenizer.eof and &tokenizer.charAt(1) notin {'0' .. '9'}:
# FIXME: `consumeNumeric` is stupid and will return zero when it can't parse a number ahead.
# Fix that crap, this is a temporary solution.
debug "tokenizer: wanted to get numeric unicode codepoint, got `" & &tokenizer.charAt(1) & "` instead."
debug "tokenizer: wanted to get numeric unicode codepoint, got `" &
&tokenizer.charAt(1) & "` instead."
return

let numeric = tokenizer.consumeNumeric(negative = false)
Expand All @@ -211,37 +212,40 @@ proc consumeBackslash*(tokenizer: Tokenizer): Result[char, MalformedStringReason
debug "tokenizer: unicode escape is followed by non-number, setting codepoint to zero"
0'i32
else:
debug "tokenizer: unicode escape is followed by a number: " & $(&numeric.intVal)
debug "tokenizer: unicode escape is followed by a number: " &
$(&numeric.intVal)
&numeric.intVal

if uniHex < 0'i32:
return err(MalformedStringReason.UnicodeEscapeIntTooSmall)

if uniHex >= 0x10FFFF'i32:
return err(MalformedStringReason.UnicodeEscapeIntTooBig)

if tokenizer.eof:
debug "tokenizer: hit EOF before seeing closing bracket of unicode escape codepoint"
return err(MalformedStringReason.BadUnicodeEscape)

tokenizer.pos -= 1 # FIXME: this is stupid. For some reason the tokenizer goes a bit too ahead after consuming a number,
# so we have to manually rewind it

tokenizer.pos -= 1
# FIXME: this is stupid. For some reason the tokenizer goes a bit too ahead after consuming a number,
# so we have to manually rewind it
let brace = tokenizer.consume()
if brace != '}':
debug "tokenizer: expected `}`, got `" & brace & "` instead when interpreting unicode escape codepoint"
debug "tokenizer: expected `}`, got `" & brace &
"` instead when interpreting unicode escape codepoint"
return err(MalformedStringReason.BadUnicodeEscape)
else:
debug "tokenizer: got `}` to successfully complete off unicode escape codepoint"
tokenizer.pos += 1 # FIXME: same crap as above

return ok(parseHexStr($uniHex)[0])
else:
debug "tokenizer: got ANSI escape `\\" & &tokenizer.charAt() & '`'
debug "tokenizer: got ANSI escape `\\" & &tokenizer.charAt() & '`'
return ok(('\\' & &tokenizer.charAt())[0])

proc consumeIdentifier*(tokenizer: Tokenizer): Token =
debug "tokenizer: consume identifier"
var
var
ident: string
containsUnicodeEsc = false

Expand All @@ -261,10 +265,12 @@ proc consumeIdentifier*(tokenizer: Tokenizer): Token =
ident &= &codepoint
else:
break

if not Keywords.contains(ident):
debug "tokenizer: consumed identifier \"" & ident & "\""
Token(kind: TokenKind.Identifier, ident: ident, containsUnicodeEsc: containsUnicodeEsc)
Token(
kind: TokenKind.Identifier, ident: ident, containsUnicodeEsc: containsUnicodeEsc
)
else:
let keyword = Keywords[ident]
debug "tokenizer: consumed keyword: " & $keyword
Expand Down Expand Up @@ -345,7 +351,7 @@ proc consumeString*(tokenizer: Tokenizer): Token =

str &= c
tokenizer.advance()

if not malformed and not str.endsWith(closesWith):
debug "tokenizer: string does not end with expected closing character"
malformed = true
Expand All @@ -357,7 +363,12 @@ proc consumeString*(tokenizer: Tokenizer): Token =
else:
warn "tokenizer: consumed malformed string: " & str

Token(kind: TokenKind.String, malformed: malformed, str: str, strMalformedReason: malformationReason)
Token(
kind: TokenKind.String,
malformed: malformed,
str: str,
strMalformedReason: malformationReason,
)

proc consumeComment*(tokenizer: Tokenizer, multiline: bool = false): Token =
debug "tokenizer: consuming comment"
Expand Down
1 change: 1 addition & 0 deletions src/bali/internal/uniconv.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

9 changes: 6 additions & 3 deletions src/bali/runtime/interpreter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ proc generateInternalIR*(runtime: Runtime) =
ident = runtime.vm.registers.callArgs.pop()
index = uint(&getInt(runtime.vm.registers.callArgs.pop()))
storeAt = uint(&getInt(runtime.vm.registers.callArgs.pop()))

let atom = runtime.vm.stack[index]
# FIXME: weird bug with mirage, `get` returns a NULL atom.

Expand All @@ -774,13 +774,16 @@ proc generateInternalIR*(runtime: Runtime) =

for typ in runtime.types:
if typ.singletonId == index:
debug "runtime: singleton ID for type `" & typ.name & "` matches field access index"
debug "runtime: singleton ID for type `" & typ.name &
"` matches field access index"
for field, member in typ.members:
if field == &ident.getStr():
debug "runtime: found field in singleton `" & typ.name & "`: " & field
if member.isFn:
error "runtime: FIXME: field access into functions is not supported yet!"
runtime.vm.typeError("FIXME: Field access into functions is not supported yet!")
runtime.vm.typeError(
"FIXME: Field access into functions is not supported yet!"
)
return
else:
runtime.vm.addAtom(member.atom(), storeAt)
Expand Down
4 changes: 3 additions & 1 deletion src/bali/runtime/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ proc registerType*[T](runtime: Runtime, name: string, prototype: typedesc[T]) =
runtime.types[typIdx].constructor(),
)

proc setProperty*[T](runtime: Runtime, prototype: typedesc[T], name: string, value: MAtom) =
proc setProperty*[T](
runtime: Runtime, prototype: typedesc[T], name: string, value: MAtom
) =
for i, typ in runtime.types:
if typ.proto == hash($prototype):
runtime.types[i].members[name] = initAtomOrFunction[NativeFunction](value)
Expand Down
11 changes: 7 additions & 4 deletions tests/runtime/interop.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ import bali/grammar/prelude
import bali/runtime/prelude
import bali/internal/sugar

proc main {.inline.} =
let parser = newParser("""
proc main() {.inline.} =
let parser = newParser(
"""
let greeting = greet("tray")
console.log(greeting)
""")
"""
)
let ast = parser.parse()
let runtime = newRuntime("interop.js", ast)

runtime.defineFn(
"greet",
proc =
proc() =
let arg = runtime.ToString(&runtime.argument(1))
ret str("Hi there, " & arg)
,
)

runtime.run()
Expand Down
20 changes: 9 additions & 11 deletions tests/runtime/prototypes-001.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@ import bali/runtime/abstract/coercion
import mirage/atom
import pretty

let parser = newParser("""
let parser = newParser(
"""
console.log(EpicClas)
EpicClass.die()
""") # I've grown tired of manually writing the AST :(
"""
) # I've grown tired of manually writing the AST :(

let program = parser.parse()

type
EpicClass* = object
myName*: string
type EpicClass* = object
myName*: string

var runtime = newRuntime("t001.js", program)
runtime.registerType(
prototype = EpicClass,
name = "EpicClass"
)
runtime.registerType(prototype = EpicClass, name = "EpicClass")
runtime.defineFn(
EpicClass,
"die",
proc =
proc() =
echo "Oooops! You killed the engine by invoking that!"
quit(0)
quit(0),
)
runtime.run()
19 changes: 9 additions & 10 deletions tests/runtime/prototypes_001.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@ import bali/runtime/abstract/coercion
import mirage/atom
import pretty, colored_logger

let parser = newParser("""
let parser = newParser(
"""
console.log(EpicClass.myName)
""") # I've grown tired of manually writing the AST :(
"""
) # I've grown tired of manually writing the AST :(

let program = parser.parse()
print program

addHandler newColoredLogger()
setLogFilter(lvlAll)

type
EpicClass* = object
myName*: string = "Deine Mutter"
type EpicClass* = object
myName*: string = "Deine Mutter"

var runtime = newRuntime("t001.js", program)
runtime.registerType(
prototype = EpicClass,
name = "EpicClass"
)
runtime.registerType(prototype = EpicClass, name = "EpicClass")
runtime.setProperty(EpicClass, "myName", str "Deine Mutter")
runtime.defineFn(
EpicClass,
"die",
proc =
proc() =
quit "Oooops! You killed the engine by invoking that!"
,
)
runtime.run()

0 comments on commit 63e825e

Please sign in to comment.