Skip to content

Commit

Permalink
[Refactor] Use new token names elsewhere
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyTy committed Oct 11, 2023
1 parent 1e0f9cf commit 9354169
Show file tree
Hide file tree
Showing 12 changed files with 507 additions and 495 deletions.
76 changes: 38 additions & 38 deletions source/compiler/lexer.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ class Lexer {
if (_8 < 58) {
var p = position
_8 = get_8(p)
var found: Token = Token.LInt
var found: Token = Token.Integer
while
_8 >= 48 && _8 <= 57 // 0...9
{
Expand All @@ -514,7 +514,7 @@ class Lexer {
{
_8 = get_8(++p)
}
found = Token.LFloat
found = Token.FloatingPoint
}

// exponent
Expand All @@ -530,7 +530,7 @@ class Lexer {
{
_8 = get_8(++p)
}
found = Token.LFloat
found = Token.FloatingPoint
}

var m = Meta.Default
Expand Down Expand Up @@ -649,9 +649,9 @@ class Lexer {
"try" : Token.KTry,
"var" : Token.KVar,
"while" : Token.KWhile,
"and" : Token.OpBoolAnd,
"or" : Token.OpBoolOr,
"not" : Token.OpNot,
"and" : Token.LogicalAnd,
"or" : Token.LogicalOr,
"not" : Token.KNot,
"is" : Token.KIs
]

Expand All @@ -661,31 +661,31 @@ class Lexer {
"@".charCodeAt(0) : Token.At,
"$".charCodeAt(0) : Token.Query,
"#".charCodeAt(0) : Token.Sharp,
"!".charCodeAt(0) : Token.OpNot, // TODO
"%".charCodeAt(0) : Token.OpMod,
"&".charCodeAt(0) : Token.OpAnd,
"(".charCodeAt(0) : Token.POpen,
")".charCodeAt(0) : Token.PClose,
"*".charCodeAt(0) : Token.OpMult,
"+".charCodeAt(0) : Token.OpAdd,
"!".charCodeAt(0) : Token.KNot, // TODO
"%".charCodeAt(0) : Token.Remainder,
"&".charCodeAt(0) : Token.BitwiseAnd,
"(".charCodeAt(0) : Token.CallOpen,
")".charCodeAt(0) : Token.CallClose,
"*".charCodeAt(0) : Token.Multiply,
"+".charCodeAt(0) : Token.Add,
",".charCodeAt(0) : Token.Comma,
"-".charCodeAt(0) : Token.OpSub,
"-".charCodeAt(0) : Token.Subtract,
".".charCodeAt(0) : Token.Dot,
"/".charCodeAt(0) : Token.OpDiv,
"/".charCodeAt(0) : Token.Divide,
":".charCodeAt(0) : Token.Colon,
";".charCodeAt(0) : Token.Semicolon,
"<".charCodeAt(0) : Token.OpLt,
"=".charCodeAt(0) : Token.OpAssign,
">".charCodeAt(0) : Token.OpGt,
"<".charCodeAt(0) : Token.Less,
"=".charCodeAt(0) : Token.Assign,
">".charCodeAt(0) : Token.Greater,
"?".charCodeAt(0) : Token.Question,
"[".charCodeAt(0) : Token.BkOpen,
"\\".charCodeAt(0) : Token.OpIntDiv,
"]".charCodeAt(0) : Token.BkClose,
"^".charCodeAt(0) : Token.OpXor,
"{".charCodeAt(0) : Token.BrOpen,
"|".charCodeAt(0) : Token.OpOr,
"}".charCodeAt(0) : Token.BrClose,
"~".charCodeAt(0) : Token.OpNegBits
"[".charCodeAt(0) : Token.IndexOpen,
"\\".charCodeAt(0) : Token.IntegerDivide,
"]".charCodeAt(0) : Token.IndexClose,
"^".charCodeAt(0) : Token.BitwiseXor,
"{".charCodeAt(0) : Token.BlockOpen,
"|".charCodeAt(0) : Token.BitwiseOr,
"}".charCodeAt(0) : Token.BlockClose,
"~".charCodeAt(0) : Token.BitwiseNot
]

for key in ops8.keys() {
Expand All @@ -697,18 +697,18 @@ class Lexer {
// `Hash = charCodeAt(0) + charCodeAt(1) * 256`
let ops16: [Int : Token] =
[
11051 : Token.OpIncrement, // ++
11565 : Token.OpDecrement, // --
15420 : Token.OpShl, // <<
15649 : Token.OpNotEq, // !=
15676 : Token.OpLte, // <=
15677 : Token.OpEq, // ==
15678 : Token.OpGte, // >=
15934 : Token.OpShr, // >>
31868 : Token.OpBoolOr, // ||
9766 : Token.OpBoolAnd, // &&
15933 : Token.OpArrow, // =>
11839 : Token.OpChain // ?.
11051 : Token.Increment, // ++
11565 : Token.Decrement, // --
15420 : Token.BitwiseLeftShift, // <<
15649 : Token.Unequal, // !=
15676 : Token.LessOrEqual, // <=
15677 : Token.Equal, // ==
15678 : Token.GreaterOrEqual, // >=
15934 : Token.BitwiseRightShift, // >>
31868 : Token.LogicalOr, // ||
9766 : Token.LogicalAnd, // &&
15933 : Token.RightArrow, // =>
11839 : Token.OptionalChain // ?.
]

// TODO automate as macro
Expand Down
72 changes: 36 additions & 36 deletions source/compiler/normalizer.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ class Normalizer {
let eb = nodeToExpression(b)

// TODO refactor `if op` to `switch op`
if op == Token.OpAdd { switch ea {
if op == Token.Add { switch ea {
case String(sa): switch eb {
// `"abc" + "dce" = "abcdce"`
case String(sb): return Expression.String(sa + sb)
Expand All @@ -519,7 +519,7 @@ class Normalizer {

// TODO use nested pattern matching here
// `case ea, op, eb:`
if op == Token.OpBoolOr { switch ea {
if op == Token.LogicalOr { switch ea {
case True: switch eb {
// `true || false = true`
case False: return Expression.True
Expand All @@ -534,7 +534,7 @@ class Normalizer {
}
}}

if op == Token.OpBoolAnd {
if op == Token.LogicalAnd {
switch ea {
case True: switch eb {
// `true && false = false`
Expand All @@ -551,7 +551,7 @@ class Normalizer {
}}

// `a == b`
if op == Token.OpEq {
if op == Token.Equal {
switch ea {
case True: switch eb {
// `true == false = false`
Expand Down Expand Up @@ -600,7 +600,7 @@ class Normalizer {
}}

// `a != b`
if op == Token.OpNotEq {
if op == Token.Unequal {
switch ea {
case True: switch eb {
// `true != false = true`
Expand Down Expand Up @@ -735,8 +735,8 @@ class Normalizer {
block.push(Statement.Const(i, Expression.Int(-1), typer.typeInt))
block.push(Statement.While(
Expression.Binop(
Expression.Parenthesis(Expression.Binop(Expression.Ident(i, typer.typeInt), Token.OpAdd, Expression.Int(1), typer.typeInt)),
Token.OpLt,
Expression.Parenthesis(Expression.Binop(Expression.Ident(i, typer.typeInt), Token.Add, Expression.Int(1), typer.typeInt)),
Token.Less,
Expression.Ident(value, valueType),
typer.typeBool),
Statement.Block([
Expand Down Expand Up @@ -776,7 +776,7 @@ class Normalizer {
let resultType = typer.types.get(nullable)
return Expression.Call(Expression.Parenthesis(Expression.Function(null, Statement.Block([
Statement.Const(result, nodeToExpression(nullable), resultType),
Statement.If(Expression.Binop(Expression.Ident(result, resultType), Token.OpNotEq, Expression.Null(resultType), typer.typeBool), Statement.Return(Expression.Ident(result, resultType)), null),
Statement.If(Expression.Binop(Expression.Ident(result, resultType), Token.Unequal, Expression.Null(resultType), typer.typeBool), Statement.Return(Expression.Ident(result, resultType)), null),
Statement.Return(nodeToExpression(otherwise))
]), [], [], Type.Function([], typer.types.get(otherwise), false))), [], typer.types.get(otherwise),
CallKind.Function
Expand All @@ -785,7 +785,7 @@ class Normalizer {

case Unop(op, postfix, e):
switch op {
case OpIncrement | OpDecrement:
case Increment | Decrement:
var source: Node? = typer.parents.get(e)
if let source = source {
switch source {
Expand All @@ -797,7 +797,7 @@ class Normalizer {

let expression = nodeToExpression(e)
switch op {
case OpNot:
case KNot:
switch expression {
// `not true = false`
case True: return Expression.False
Expand Down Expand Up @@ -849,7 +849,7 @@ class Normalizer {
var parens = Expression.Parenthesis(nodeToExpression(econd[0]))
var i = 1
while i < econd.length {
parens = Expression.Binop(parens, Token.OpBoolAnd, Expression.Parenthesis(nodeToExpression(econd[i])), typer.typeBool)
parens = Expression.Binop(parens, Token.LogicalAnd, Expression.Parenthesis(nodeToExpression(econd[i])), typer.typeBool)
i++
}
return Expression.If(parens, nodeToExpression(eif), nodeToExpression(eelse))
Expand All @@ -863,7 +863,7 @@ class Normalizer {
case As(e, kind, t): switch kind {
case Question:
console.log('`as?` is not yet supported by normalizer, only `as!`')
case OpNot:
case KNot:
return Expression.UnsafeBitCast(nodeToExpression(e), typer.types.get(t))
case _:
console.log('`as` is not yet supported by normalizer, only `as!`')
Expand Down Expand Up @@ -1070,7 +1070,7 @@ class Normalizer {
let values = []
for f in fields { switch f {
// `ValueName = b`
case Binop(a, op, b): // op == OpAssign
case Binop(a, op, b): // op == Assign
switch a {
case NodeTypeValue(t): switch t {
case Type(name):
Expand Down Expand Up @@ -1122,7 +1122,7 @@ class Normalizer {
case Var(name, t, expr, const):
a.push(unique(name, arg))
types.push(t)
if expr != null {
if (expr != null) {
defaults.push(nodeToExpression(expr))
} else {
defaults.push(null)
Expand Down Expand Up @@ -1274,7 +1274,7 @@ class Normalizer {
case Var(name, t, expr, const):
a.push(unique(name, arg))
types.push(t)
if (expr != null) {
if expr != null {
defaults.push(nodeToExpression(expr))
} else {
defaults.push(null)
Expand Down Expand Up @@ -1504,7 +1504,7 @@ class Normalizer {
Statement.Var(i, Expression.Int(0), typer.typeInt),
Statement.Var(it, Expression.Int(0), typer.typeInt),
Statement.While(
Expression.Binop(Expression.Ident(it, typer.typeInt), Token.OpLt, Expression.Int(s), typer.typeBool),
Expression.Binop(Expression.Ident(it, typer.typeInt), Token.Less, Expression.Int(s), typer.typeBool),
Statement.Block([
Statement.Assign(Expression.Ident(i, typer.typeInt), Expression.Ident(it, typer.typeInt), null),
Statement.Increment(Expression.Ident(it, typer.typeInt)),
Expand Down Expand Up @@ -1547,7 +1547,7 @@ class Normalizer {
Statement.While(
Expression.Binop(
Expression.Ident(it, overType),
Token.OpLt,
Token.Less,
Expression.Ident(finish, overType),
typer.typeBool
),
Expand Down Expand Up @@ -1583,7 +1583,7 @@ class Normalizer {
Statement.Var(it, nodeToExpression(over), typer.typeInt),
Statement.Var(finish, nodeToExpression(range), typer.typeInt),
Statement.While(
Expression.Binop(Expression.Ident(it, typer.typeInt), Token.OpLt, Expression.Ident(finish, typer.typeInt), typer.typeBool),
Expression.Binop(Expression.Ident(it, typer.typeInt), Token.Less, Expression.Ident(finish, typer.typeInt), typer.typeBool),
Statement.Block([
Statement.Assign(Expression.Ident(i, typer.typeInt), Expression.Ident(it, typer.typeInt), null),
Statement.Increment(Expression.Ident(it, typer.typeInt)),
Expand Down Expand Up @@ -1621,7 +1621,7 @@ class Normalizer {
),
Statement.While(
// `while it < at`
Expression.Binop(Expression.Ident(it, typer.typeInt), Token.OpLt, Expression.Ident(finish, typer.typeInt), typer.typeBool),
Expression.Binop(Expression.Ident(it, typer.typeInt), Token.Less, Expression.Ident(finish, typer.typeInt), typer.typeBool),
// `{`
Statement.Block([
// `var name = over[it]` TODO `name` must be nullable
Expand Down Expand Up @@ -1688,7 +1688,7 @@ class Normalizer {
var parens = Expression.Parenthesis(nodeToExpression(econd[0]))
var i = 1
while i < econd.length {
parens = Expression.Binop(parens, Token.OpBoolAnd, Expression.Parenthesis(nodeToExpression(econd[i])), typer.typeBool)
parens = Expression.Binop(parens, Token.LogicalAnd, Expression.Parenthesis(nodeToExpression(econd[i])), typer.typeBool)
i++
}
return Statement.If(parens, nodeToStatement(eif), nodeToStatement(eelse))
Expand Down Expand Up @@ -1719,9 +1719,9 @@ class Normalizer {
block.push(Statement.Var(name, Expression.Null(typer.types.get(expr)), typer.types.get(expr)))
block.push(Statement.If(
Expression.Binop(
Expression.Binop(Expression.Ident(step, typer.typeInt), Token.OpEq, Expression.Int(current + 0), typer.typeBool),
Token.OpBoolAnd,
Expression.Binop(Expression.Ident(last, lastType), Token.OpNotEq, nullable? Expression.Null(lastType) : Expression.False, typer.typeBool),
Expression.Binop(Expression.Ident(step, typer.typeInt), Token.Equal, Expression.Int(current + 0), typer.typeBool),
Token.LogicalAnd,
Expression.Binop(Expression.Ident(last, lastType), Token.Unequal, nullable? Expression.Null(lastType) : Expression.False, typer.typeBool),
typer.typeBool
),
Statement.Block([
Expand All @@ -1737,9 +1737,9 @@ class Normalizer {
block.push(Statement.Var(next, Expression.Null(nextType), nextType))
block.push(Statement.If(
Expression.Binop(
Expression.Binop(Expression.Ident(step, typer.typeInt), Token.OpEq, Expression.Int(current + 0), typer.typeBool),
Token.OpBoolAnd,
Expression.Binop(Expression.Ident(last, lastType), Token.OpNotEq, nullable? Expression.Null(lastType) : Expression.False, typer.typeBool),
Expression.Binop(Expression.Ident(step, typer.typeInt), Token.Equal, Expression.Int(current + 0), typer.typeBool),
Token.LogicalAnd,
Expression.Binop(Expression.Ident(last, lastType), Token.Unequal, nullable? Expression.Null(lastType) : Expression.False, typer.typeBool),
typer.typeBool
),
Statement.Block([
Expand All @@ -1754,16 +1754,16 @@ class Normalizer {
// Last step
block.push(Statement.If(
Expression.Binop(
Expression.Binop(Expression.Ident(step, typer.typeInt), Token.OpEq, Expression.Int(econd.length - 1), typer.typeBool),
Token.OpBoolAnd,
Expression.Binop(Expression.Ident(last, lastType), Token.OpNotEq, nullable? Expression.Null(lastType) : Expression.False, typer.typeBool),
Expression.Binop(Expression.Ident(step, typer.typeInt), Token.Equal, Expression.Int(econd.length - 1), typer.typeBool),
Token.LogicalAnd,
Expression.Binop(Expression.Ident(last, lastType), Token.Unequal, nullable? Expression.Null(lastType) : Expression.False, typer.typeBool),
typer.typeBool
),
Statement.Assign(Expression.Ident(step, typer.typeInt), Expression.Int(econd.length), null), null))

// if (step == steps)
block.push(Statement.If(
Expression.Binop(Expression.Ident(step, typer.typeInt), Token.OpEq, Expression.Int(econd.length), typer.typeBool),
Expression.Binop(Expression.Ident(step, typer.typeInt), Token.Equal, Expression.Int(econd.length), typer.typeBool),
nodeToStatement(eif), nodeToStatement(eelse)))

return Statement.Block(block)
Expand Down Expand Up @@ -1800,7 +1800,7 @@ class Normalizer {

case Binop(a, op, b):
switch op {
case OpAssign:
case Assign:
var source: Node? = typer.parents.get(a)
if let source = source {
switch source {
Expand Down Expand Up @@ -1829,7 +1829,7 @@ class Normalizer {

case Unop(op, postfix, e):
switch op {
case OpIncrement | OpDecrement:
case Increment | Decrement:
var source: Node? = typer.parents.get(e)
if let source = source {
switch source {
Expand All @@ -1839,8 +1839,8 @@ class Normalizer {
}
}
switch op {
case OpIncrement: return Statement.Increment(nodeToExpression(e))
case OpDecrement: return Statement.Decrement(nodeToExpression(e))
case Increment: return Statement.Increment(nodeToExpression(e))
case Decrement: return Statement.Decrement(nodeToExpression(e))
case _: // Optimize away
}
case Parenthesis(inner):
Expand Down Expand Up @@ -1872,7 +1872,7 @@ class Normalizer {
case Dot(expr, name): return Expression.Dot(nodeToExpression(expr), name, null)
case DotUpper(expr, name): return Expression.Dot(nodeToExpression(expr), name, null)
case Binop(a, op, b): switch op {
case OpOr: return Expression.Binop(toCase(a), Token.OpOr, toCase(b), null)
case BitwiseOr: return Expression.Binop(toCase(a), Token.BitwiseOr, toCase(b), null)
case _: console.error('(internal warning) Unknown native case binop kind:', e, typer.parents.get(e))
}
case Call(e, args, argNames): switch e {
Expand Down Expand Up @@ -1979,7 +1979,7 @@ class Normalizer {
cx.push(getTag(typer.parents.get(expr), name))
case Binop(a, op, b):
switch op {
case OpOr:
case BitwiseOr:
addTag(a)
addTag(b)
case _: console.error('(internal warning) Unknown case binop kind:', e, typer.parents.get(e))
Expand Down
Loading

0 comments on commit 9354169

Please sign in to comment.