Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/Translation/ForthToMLIR/ForthToMLIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cctype>
#include <cmath>

using namespace mlir;
using namespace mlir::forth;
Expand Down Expand Up @@ -958,15 +959,21 @@ LogicalResult ForthParser::parseBody(Value &stack) {

if (currentToken.kind == Token::Kind::Number) {
Location tokenLoc = getLoc();
int64_t value = std::stoll(currentToken.text);
StringRef text = currentToken.text;
text.consume_front("+");
int64_t value;
if (text.getAsInteger(10, value))
return emitError("integer literal out of range for i64");
stack = builder
.create<forth::ConstantOp>(tokenLoc, stackType, stack,
builder.getI64IntegerAttr(value))
.getResult();
consume();
} else if (currentToken.kind == Token::Kind::Float) {
Location tokenLoc = getLoc();
double value = std::stod(currentToken.text);
double value;
if (!llvm::to_float(currentToken.text, value) || std::isinf(value))
return emitError("float literal out of range for f64");
stack = builder
.create<forth::ConstantOp>(tokenLoc, stackType, stack,
builder.getF64FloatAttr(value))
Expand Down
5 changes: 4 additions & 1 deletion test/Translation/Forth/basic-literals.forth
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
\ CHECK-NEXT: forth.constant %{{.*}}(42 : i64)
\ CHECK-NEXT: forth.constant %{{.*}}(-7 : i64)
\ CHECK-NEXT: forth.constant %{{.*}}(0 : i64)
\ CHECK-NEXT: forth.constant %{{.*}}(5 : i64)
\ CHECK-NEXT: forth.constant %{{.*}}(9223372036854775807 : i64)
\ CHECK-NEXT: forth.constant %{{.*}}(-9223372036854775808 : i64)
\! kernel main
42 -7 0
42 -7 0 +5 9223372036854775807 -9223372036854775808
4 changes: 4 additions & 0 deletions test/Translation/Forth/float-literal-overflow-error.forth
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\ RUN: %not %warpforth-translate --forth-to-mlir %s 2>&1 | %FileCheck %s
\ CHECK: {{.*}}float-literal-overflow-error.forth:4:5: error: float literal out of range for f64
\! kernel main
1.0 -1e999
4 changes: 3 additions & 1 deletion test/Translation/Forth/float-literals.forth
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
\ CHECK-NEXT: forth.constant %{{.*}}(5.000000e+00 : f64)
\ CHECK-NEXT: forth.constant %{{.*}}(1.500000e+00 : f64)
\ CHECK-NEXT: forth.constant %{{.*}}(2.500000e+02 : f64)
\ CHECK-NEXT: forth.constant %{{.*}}(1.7976931348623157E+308 : f64)
\ CHECK-NEXT: forth.constant %{{.*}}(0.000000e+00 : f64)
\! kernel main
3.14 -2.0 1.0e-5 1e3 1E3 .5 -.5 5. +1.5 2.5e+2
3.14 -2.0 1.0e-5 1e3 1E3 .5 -.5 5. +1.5 2.5e+2 1.7976931348623157e308 1e-400
4 changes: 4 additions & 0 deletions test/Translation/Forth/integer-literal-overflow-error.forth
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\ RUN: %not %warpforth-translate --forth-to-mlir %s 2>&1 | %FileCheck %s
\ CHECK: {{.*}}integer-literal-overflow-error.forth:4:3: error: integer literal out of range for i64
\! kernel main
1 9223372036854775808
4 changes: 4 additions & 0 deletions test/Translation/Forth/integer-literal-underflow-error.forth
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\ RUN: %not %warpforth-translate --forth-to-mlir %s 2>&1 | %FileCheck %s
\ CHECK: {{.*}}integer-literal-underflow-error.forth:4:3: error: integer literal out of range for i64
\! kernel main
1 -9223372036854775809
Loading