Skip to content

Commit

Permalink
[Refactor] TODOs and cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyTy committed Sep 18, 2024
1 parent 1ff8ca6 commit 1126ae1
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 39 deletions.
3 changes: 2 additions & 1 deletion library/browser/html.hexa
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// The MIT License
//
// Copyright (C) 2021-2023 Oleh Petrenko
// Copyright (C) 2021-2024 Oleh Petrenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,6 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

/// Global browser functions and values
declare let window: {
/// Raw global window object
window: Any
Expand Down
5 changes: 3 additions & 2 deletions library/c/runtime.hexa
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// The MIT License
//
// Copyright (C) 2022 Oleh Petrenko
// Copyright (C) 2022-2024 Oleh Petrenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,9 +26,10 @@
fun
hexaRuntimeInit(): Void
{

// TODO
}

// TODO description and documentation
@struct class RuntimeAllocated {
}

Expand Down
4 changes: 2 additions & 2 deletions library/c/string.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class String {
}

static fun fromUTF8z(string: ConstArrayPointer<UInt8>): String {
// Note: must use own string length implementation cause incompatible with freestanding targets
// NOTE must use own string length implementation cause incompatible with freestanding targets
let size: Int = countUTF8z(string)
// TODO test type check that constructor is private
// TODO use stack allocated array for small strings or .bss 64k one if size < 64k
Expand Down Expand Up @@ -307,7 +307,7 @@ class String {

static fun fromInt32(value: Int): String {
// TODO just [0, ..., 0] N times
// Note: reserves 2 bytes for null-terminator
// NOTE reserves 2 bytes for null-terminator
// ^ for some reason required despite being UInt16-array
// TODO ^ test it more, comment/overwrite `bytes[i] = 0` etc
var bytes: ArrayByValue<UInt16, 12> = [0]
Expand Down
4 changes: 2 additions & 2 deletions library/nodejs/nodejs.hexa
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// The MIT License
//
// Copyright (C) 2021-2023 Oleh Petrenko
// Copyright (C) 2021-2024 Oleh Petrenko
// Copyright (C) 2018 Bogdan Danylchenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
Expand All @@ -25,7 +25,7 @@ declare fun require(package: String): Any
declare let __dirname: String
declare let __filename: String

// TODO reqire(Buffer)?
// TODO reqire(Buffer)? node:Buffer `require('node:buffer').Buffer`
@rename("Buffer")
@native("Buffer")
declare class Buffer {
Expand Down
1 change: 1 addition & 0 deletions source/cli/init.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,6 @@ fun init() {

// Detects `y`, `yes` and empty answer as `true`
fun isYes(answer: String): Bool {
// TODO code gen must cache this array globally for .includes without allocation
return ['y','yes',''].includes(answer.toLowerCase().trim())
}
7 changes: 4 additions & 3 deletions source/compiler/lexer.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Lexer {
let meta: [Meta] = []
// ^ try the compressed idea for meta's at least TODO
// TODO no zeroing of this buffer when parser is *non*-look-ahead
let tokens = Buffer.alloc(len + 1) // Note: 1 extra space for Token.Eof
let tokens = Buffer.alloc(len + 1) // NOTE 1 extra space for Token.Eof
// TODO ensure optimizer/typer infers `Array<T>` properly
var lines = []
var columns = []
Expand Down Expand Up @@ -245,7 +245,7 @@ class Lexer {

// Identifiers and keywords
// A-z _
// Note: non-ascii identifiers are NOT allowed
// NOTE non-ascii identifiers are NOT allowed
// TODO describe the 95 trick
if ((_8 & 95) >= 65 && (_8 & 95) <= 90) or (_8 == 95) {
let title = _8 <= 90
Expand All @@ -254,7 +254,7 @@ class Lexer {

// TODO is table lookup really faster than conditions? Bit mask possible>
do {
// Note: underscore is inside keyword map
// NOTE underscore is inside keyword map
// This excludes numbers
allLowercase = allLowercase and (_8 >= 95) // TODO `'_'.charCodeAt(0)`
p++
Expand Down Expand Up @@ -326,6 +326,7 @@ class Lexer {

// >>>
// TODO this may be done in parser
// ^ if lexer called from parser, it may set flag of not-parsing >>> in templates
if _16 == 15934 and (get_8(position + 2) == ">".charCodeAt(0)) {
add(Token.UnsignedRightShift)
position += 3
Expand Down
2 changes: 2 additions & 0 deletions source/compiler/normalizer.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -876,10 +876,12 @@ class Normalizer {
case As(e, kind, t): switch kind {
case Question:
console.log('`as?` is not yet supported by normalizer, only `as!`')
// TODO error/implement
case KNot:
return Expression.UnsafeBitCast(nodeToExpression(e), typer.types.get(t))
case _:
console.log('`as` is not yet supported by normalizer, only `as!`')
// TODO error/implement
}

case Is(e, t):
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/parser.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,7 @@ class Parser {
let token = tok()

if
// Note: don't do it like that! Some type syntax uses lowercases
// NOTE don't do it like that! Some type syntax uses lowercases
//tok() != Token.Assign and
//tok() != Token.CallClose and
//tok() != Token.Comma
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/preprocessor.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Preprocessor {
private static fun processTokens(tokens: Tokens, i: Int, project: Project): Tokens {
var i = i
var token = tokens.token[0] as! Token
let bytes = tokens.token.slice() // Note: this creates a copy with all tokens
let bytes = tokens.token.slice() // NOTE this creates a copy with all tokens
// TODO Slicing from `i` would cause allocation on ~every array `[to]` insert, any ideas?
// Maybe just allocate Array(len) + copy values upto i, so unused are null
let params = tokens.value.slice()
Expand Down
24 changes: 15 additions & 9 deletions source/compiler/typer.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -2240,14 +2240,15 @@ class Typer {

addScope(name, node)

// `a op b` like `a + b`
// `a = b` as a statement
case Binop(a, op, b):
fillExpression(node)

var parent: Node = parents.get(a)
// `a = b`
// TODO separate assign node? or just nested pattern...
if op == Token.Assign { switch parent {
if op == Token.Assign {
switch parent {
case null:
switch a {
case Ident(_): // Ok
Expand Down Expand Up @@ -2994,26 +2995,26 @@ class Typer {
// TODO why the heck `.Vname` here gives Ln1, Col 12???
// type.V here is ok, but is in the string interpolator
// ^ should be possible to set starting col/line in Lexer
// Note: `proposeSimilar` is useless for DotUpper over instances
// NOTE `proposeSimilar` is useless for DotUpper over instances
fail('Enum `\(type.name)` instance has no field `.\(n)`', node)
case ClassInstance(type):
failNonFatal('Type `\(type.name)` is defined here', type.parent)
// Note: `proposeSimilar` is useless for DotUpper over instances
// NOTE `proposeSimilar` is useless for DotUpper over instances
fail('Class `\(type.name)` has no field `.\(n)`\n\nNote that you are trying to access plain `class` instance and not `enum`', node)
case Class(type):
failNonFatal('Type `\(type.name)` is defined here', type.parent)
// Note: `proposeSimilar` is useless for DotUpper over classes
// NOTE `proposeSimilar` is useless for DotUpper over classes
fail('Class `\(type.name)` has no field `.\(n)`\n\nNote that you are trying to access `class` and not `enum`', node)
case Enum(type):
let index = type.fieldNames.indexOf(n)
// TODO `@lazy let enumName = getName(type.name)` so no need to place in every failing `if`
if index < 0 {
failNonFatal('Type `\(type.name)` is defined here', type.parent)
// Note: `proposeSimilar` but only constructors (if any)
// NOTE `proposeSimilar` but only constructors (if any)
fail('Enum `\(type.name)` has no value constructor `.\(n)`\n' + proposeSimilar(type.fieldNames.filter((element, index) => type.fieldEnumConstructor[index]), n, 'constructors'), node)
}
if type.fieldEnumConstructor[index] != true {
// Note: this case is probably impossible, i.e. .N *not* being constructor (than what?)
// NOTE this case is probably impossible, i.e. .N *not* being constructor (than what?)
fail('Enum `\(type.name)` field `.\(n)` is not a value constructor', node)
}
if let fieldType = type.fieldType[index] {
Expand All @@ -3033,6 +3034,7 @@ class Typer {
if asValue {
fail('Cannot use type `\(name)` as a value, use `\(name)()` to call constructor', node)
}

let subj = find(name)
if subj == null {
fail('Cannot find type with name `\(name)`', node)
Expand All @@ -3050,7 +3052,8 @@ class Typer {
let mod = currentModule
registerClassType(subj)
currentModule = mod
case Enum(_): registerEnumType(subj)
case Enum(_):
registerEnumType(subj)
}

if let type = types.get(subj) {
Expand Down Expand Up @@ -3465,6 +3468,7 @@ class Typer {

// TODO `null == null` should error cause no sense
if leftString != 'null', rightString != 'null' {
// TODO recommend `.address` for pointers
fail('Operator `a \(tokenString) b` requires `a` and `b` of comparable types, but got `\(leftString)` and `\(rightString)`', b)
}
}
Expand Down Expand Up @@ -3773,8 +3777,9 @@ class Typer {
}

if thisType == null {
fail('Cannot access `this` in class method', node)
fail('Cannot access `this` in the static method', node)
}

types.set(node, thisType)
parents.set(node, thisNode)

Expand All @@ -3784,6 +3789,7 @@ class Typer {

// `return e`
case Return(e):
// TODO cannot return from constructor
switch e {
case Var(name, _, _, const):
let prefix = const? 'let' : 'var'
Expand Down
15 changes: 10 additions & 5 deletions source/data/data.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ enum Node {
/// `declare fun name<params>(vars) retType { body }`
// Name is `new` for constructor
// TODO expr Node.Block (goal is *not* to `Node extends NodeBlock` but to actually limit tags)
Function(name String?, body Node, vars [Node], retType NodeType, external Bool, variadic Bool)
Function(name String?, body Node, vars [Node], returnType NodeType, external Bool, variadic Bool)


/// (vars) retType => expr
/// `(vars) retType => expr`
// TODO parser `: retType` is really needed at all?
Arrow(expr Node, vars [Node], retType NodeType)

/// [external ? declare] (const ? let : var) name nodeType [= expr]
// `varStatic` =>> `funStatic` `varPrivate` etc
/// [external ? `declare`] (const ? `let` : `var`) `name nodeType` [`= expr`]
// TODO `expr Node?`
Var(name String, varType NodeType, expr Node, const Bool, external Bool)

Expand All @@ -124,9 +124,11 @@ enum Node {
Try(expr Node, t [NodeType], v [Node], catches [Node])

/// `expr.name`
// TODO rename to Field
Dot(expr Node, name String)

/// expr.Name
// TODO rename to CapitalField
DotUpper(expr Node, name String)

/// `new T<T> { } (args)` TODO new syntax
Expand Down Expand Up @@ -178,20 +180,23 @@ enum Node {
Underscore

/// `...`
// TODO support `...expr`
Interval

/// `static` field
// TODO drop this
Static(field Node)

/// `private` field or type
// TODO drop this
Private(field Node)

/// for name in over by
/// for name in over ... range by
For(name String, over Node, by Node, range Node)

/// `nullable ?? othewise`
Elvis(nullable Node, otherwise Node)
/// `nullable ?? alternative`
Elvis(nullable Node, alternative Node)

/// `T<A, B>` but as expression
NodeTypeValue(type NodeType)
Expand Down
1 change: 1 addition & 0 deletions source/data/nice.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ enum Statement {
Assignop(name Expression, op Token, value Expression)
// TODO ^ `AssignOp`
For(name String, over Expression, by Statement)
// TODO why not separate `DoWhile`?
While(econd Expression, e Statement, pre Bool)
Increment(e Expression)
Decrement(e Expression)
Expand Down
4 changes: 2 additions & 2 deletions source/data/token.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ enum Token : Int {
BitwiseNot = 88 // ~

// Binary
// TODO Note: Op- prefix for names used cause in IR binary operators use Token as operator kind
// TODO NOTE Op- prefix for names used cause in IR binary operators use Token as operator kind
// ^ somehow create an enum subset of them?
RightArrow = 90 // =>
Assign = 91 // =
Expand Down Expand Up @@ -155,7 +155,7 @@ enum Token : Int {
case KElse: return "else"
case KEnum: return "enum"
case KExtends: return "extends"
// Note: `declare` instantly creates TypeScript vibe and must be kept
// NOTE `declare` instantly creates TypeScript vibe and must be kept
case KDeclare: return "declare"
case KFalse: return "false"
case KFor: return "for"
Expand Down
4 changes: 2 additions & 2 deletions source/main.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class Main {

if named {
let name = DataHelper.nameOf(e)
if (project.globals.has(name)) {
if project.globals.has(name) {
let data = project.data.get(e)
let already = project.data.get(project.globals.get(name))
let msg = "Global node `\(name)` declared in `\(path)` already defined at `\(already.fileName)` line `\(already.line)`"
Expand Down Expand Up @@ -619,7 +619,7 @@ class Main {
console.log("[Building \(input.name)]")
}

console.log("[Using 1 of \(require('os').cpus().length) CPU cores]")
console.log("[Using 1 of \(require('os').cpus().length) CPU threads]")
// TODO ^ fix syntax highlighting for \parenthesis pairs
}
}
Expand Down
Loading

0 comments on commit 1126ae1

Please sign in to comment.