Skip to content

Commit acae822

Browse files
committed
Feature(compiler): Support GreaterThanToken/LessThanToken
1 parent c83a8ab commit acae822

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

sandbox/do-simple-math.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
console_log("hello");
1717
console_log(doMath());
18-
console_log(true);
19-
console_log(false);
18+
// console_log(true);
19+
// console_log(false);
20+
console_log(1 > 5);
21+
console_log(1 < 5);
2022
}

src/backend/llvm/index.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import {RUNTIME_DEFINITION_FILE} from "@static-script/runtime";
1010
import {LANGUAGE_DEFINITION_FILE} from "../../constants";
1111
import {CMangler} from "./c.mangler";
1212
import {ManglerInterface} from "./mangler.interface";
13-
import {SignatureDeclaration} from "typescript";
14-
import {FunctionDeclaration} from "typescript";
1513

1614
export function passReturnStatement(parent: ts.ReturnStatement, ctx: Context, builder: llvm.IRBuilder) {
1715
if (!parent.expression) {
@@ -156,10 +154,36 @@ function buildFromBinaryExpression(
156154
loadIfNeeded(right, builder, ctx)
157155
);
158156
}
157+
case ts.SyntaxKind.GreaterThanToken: {
158+
const left = buildFromExpression(expr.left, ctx, builder);
159+
const right = buildFromExpression(expr.right, ctx, builder);
160+
161+
const leftInt = builder.createZExt(left, llvm.Type.getInt32Ty(ctx.llvmContext));
162+
const rightInt = builder.createZExt(right, llvm.Type.getInt32Ty(ctx.llvmContext));
163+
164+
return builder.createFCmpOGT(
165+
leftInt,
166+
rightInt,
167+
'cmpGT'
168+
);
169+
}
170+
case ts.SyntaxKind.LessThanToken: {
171+
const left = buildFromExpression(expr.left, ctx, builder);
172+
const right = buildFromExpression(expr.right, ctx, builder);
173+
174+
const leftInt = builder.createZExt(left, llvm.Type.getInt32Ty(ctx.llvmContext));
175+
const rightInt = builder.createZExt(right, llvm.Type.getInt32Ty(ctx.llvmContext));
176+
177+
return builder.createFCmpOLT(
178+
leftInt,
179+
rightInt,
180+
'cmpLT'
181+
);
182+
}
159183
default:
160184
throw new UnsupportedError(
161185
expr,
162-
`Unsupported BinaryExpression.operator: "${expr.kind}"`
186+
`Unsupported BinaryExpression.operator: "${expr.operatorToken.kind}"`
163187
);
164188
}
165189
}

0 commit comments

Comments
 (0)