Skip to content

Commit 9022335

Browse files
committed
Updated reference tests and resolved conflicts
2 parents cbf7483 + 1ed8cb8 commit 9022335

File tree

89 files changed

+369
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+369
-197
lines changed

integration_tests/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ RUN(NAME expr_06 LABELS cpython llvm)
135135
RUN(NAME expr_07 LABELS cpython llvm)
136136
RUN(NAME expr_08 LABELS llvm c)
137137
RUN(NAME expr_09 LABELS cpython llvm)
138+
RUN(NAME expr_10 LABELS cpython)
138139
RUN(NAME test_types_01 LABELS cpython llvm)
139140
RUN(NAME test_str_01 LABELS cpython llvm)
140141
RUN(NAME test_str_02 LABELS cpython llvm)
142+
RUN(NAME test_str_03 LABELS cpython llvm c)
141143
RUN(NAME test_list_01 LABELS cpython llvm)
142144
RUN(NAME modules_01 LABELS cpython llvm)
143145
RUN(NAME test_math LABELS cpython llvm)
@@ -182,6 +184,7 @@ RUN(NAME structs_03 LABELS llvm c)
182184
RUN(NAME test_str_to_int LABELS cpython llvm)
183185
RUN(NAME test_platform LABELS cpython llvm)
184186
RUN(NAME test_vars_01 LABELS cpython llvm)
187+
RUN(NAME test_version LABELS cpython llvm)
185188

186189
# Just CPython
187190
RUN(NAME test_builtin_bin LABELS cpython)

integration_tests/expr_05.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def main0():
2323
a = 123282374
2424
b = 32771
2525
assert test_mod(a, b) == 30643
26+
a = -5345
27+
b = -534
28+
assert a % b == -5
2629
a = -123282374
2730
b = 32771
2831
assert test_mod(a, b) == 2128

integration_tests/expr_10.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from ltypes import i32
2+
3+
def g() -> i32:
4+
return 5
5+
6+
def test_fn1():
7+
i: i32 = g()
8+
g()
9+
10+
test_fn1()

integration_tests/run_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -ex
44

5-
git clean -dfx
5+
rm -rf b1 b2 b3
66

77
# Append "-j4" or "-j" to run in parallel
88
jn=$1

integration_tests/test_str_03.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def test_new_line():
2+
print("abc\n")
3+
print("\ndef")
4+
print("x\nyz")
5+
6+
def test_int():
7+
i: i8 = 1
8+
j: i16 = 2
9+
k: i32 = 3
10+
l: i64 = 4
11+
print("abc:", i, j, k, l)
12+
13+
test_new_line()
14+
test_int()

integration_tests/test_version.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from platform import python_version
2+
3+
def test_lpython_version():
4+
print(python_version())
5+
6+
test_lpython_version()

src/bin/lpython.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ int emit_cpp(const std::string &infile,
214214
LFortran::ASR::TranslationUnit_t* asr = r1.result;
215215

216216
diagnostics.diagnostics.clear();
217-
auto res = LFortran::asr_to_cpp(al, *asr, diagnostics);
217+
auto res = LFortran::asr_to_cpp(al, *asr, diagnostics,
218+
compiler_options.platform);
218219
std::cerr << diagnostics.render(input, lm, compiler_options);
219220
if (!res.ok) {
220221
LFORTRAN_ASSERT(diagnostics.has_error())
@@ -254,7 +255,8 @@ int emit_c(const std::string &infile,
254255
LFortran::ASR::TranslationUnit_t* asr = r1.result;
255256

256257
diagnostics.diagnostics.clear();
257-
auto res = LFortran::asr_to_c(al, *asr, diagnostics);
258+
auto res = LFortran::asr_to_c(al, *asr, diagnostics,
259+
compiler_options.platform);
258260
std::cerr << diagnostics.render(input, lm, compiler_options);
259261
if (!res.ok) {
260262
LFORTRAN_ASSERT(diagnostics.has_error())

src/libasr/ASR.asdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ ttype
318318
| Pointer(ttype type)
319319
| CPtr()
320320

321-
binop = Add | Sub | Mul | Div | Pow
321+
binop = Add | Sub | Mul | Div | Pow | BitAnd | BitOr | BitXor | BitLShift | BitRShift
322322

323323
logicalbinop = And | Or | Xor | NEqv | Eqv
324324

src/libasr/asr_utils.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ bool is_op_overloaded(ASR::binopType op, std::string& intrinsic_op_name,
388388
}
389389
break;
390390
}
391+
default: {
392+
throw LFortranException("Binary operator '" + ASRUtils::binop_to_str(op) + "' not supported yet");
393+
}
391394
}
392395
if( result && curr_scope->get_symbol(intrinsic_op_name) == nullptr ) {
393396
result = false;

src/libasr/asr_utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ static inline std::string binop_to_str(const ASR::binopType t) {
261261
case (ASR::binopType::Sub): { return " - "; }
262262
case (ASR::binopType::Mul): { return "*"; }
263263
case (ASR::binopType::Div): { return "/"; }
264+
case (ASR::binopType::BitAnd): { return "&"; }
265+
case (ASR::binopType::BitOr): { return "|"; }
266+
case (ASR::binopType::BitXor): { return "^"; }
267+
case (ASR::binopType::BitLShift): { return "<<"; }
268+
case (ASR::binopType::BitRShift): { return ">>"; }
264269
default : throw LFortranException("Cannot represent the binary operator as a string");
265270
}
266271
}

src/libasr/codegen/asr_to_c.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
5959
{
6060
public:
6161

62-
ASRToCVisitor(diag::Diagnostics &diag) : BaseCCPPVisitor(diag,
63-
false, false, true) {}
62+
ASRToCVisitor(diag::Diagnostics &diag, Platform &platform)
63+
: BaseCCPPVisitor(diag, platform, false, false, true) {}
6464

6565
std::string convert_variable_decl(const ASR::Variable_t &v)
6666
{
@@ -376,7 +376,13 @@ R"(
376376
case 1: { return "%d"; }
377377
case 2: { return "%d"; }
378378
case 4: { return "%d"; }
379-
case 8: { return "%lli"; }
379+
case 8: {
380+
if (platform == Platform::Linux) {
381+
return "%li";
382+
} else {
383+
return "%lli";
384+
}
385+
}
380386
default: { throw LFortranException("Integer kind not supported"); }
381387
}
382388
}
@@ -415,8 +421,8 @@ R"(
415421
std::vector<std::string> v;
416422
for (size_t i=0; i<x.n_values; i++) {
417423
this->visit_expr(*x.m_values[i]);
418-
ASR::ttype_t* valuei_type = ASRUtils::expr_type(x.m_values[i]);
419-
out += get_print_type(valuei_type, ASR::is_a<ASR::ArrayRef_t>(*x.m_values[i]));
424+
ASR::ttype_t* value_type = ASRUtils::expr_type(x.m_values[i]);
425+
out += get_print_type(value_type, ASR::is_a<ASR::ArrayRef_t>(*x.m_values[i]));
420426
if (i+1!=x.n_values) {
421427
out += " ";
422428
}
@@ -435,11 +441,11 @@ R"(
435441
};
436442

437443
Result<std::string> asr_to_c(Allocator &al, ASR::TranslationUnit_t &asr,
438-
diag::Diagnostics &diagnostics)
444+
diag::Diagnostics &diagnostics, Platform &platform)
439445
{
440446
pass_unused_functions(al, asr, true);
441447
pass_replace_class_constructor(al, asr);
442-
ASRToCVisitor v(diagnostics);
448+
ASRToCVisitor v(diagnostics, platform);
443449
try {
444450
v.visit_asr((ASR::asr_t &)asr);
445451
} catch (const CodeGenError &e) {

src/libasr/codegen/asr_to_c.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
#define LFORTRAN_ASR_TO_C_H
33

44
#include <libasr/asr.h>
5+
#include <libasr/utils.h>
56

67
namespace LFortran {
78

89
Result<std::string> asr_to_c(Allocator &al, ASR::TranslationUnit_t &asr,
9-
diag::Diagnostics &diagnostics);
10+
diag::Diagnostics &diagnostics, Platform &platform);
1011

1112
} // namespace LFortran
1213

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Derived>
6969
Derived& self() { return static_cast<Derived&>(*this); }
7070
public:
7171
diag::Diagnostics &diag;
72+
Platform platform;
7273
std::string src;
7374
int indentation_level;
7475
int indentation_spaces;
@@ -89,8 +90,9 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Derived>
8990

9091
SymbolTable* global_scope;
9192

92-
BaseCCPPVisitor(diag::Diagnostics &diag,
93+
BaseCCPPVisitor(diag::Diagnostics &diag, Platform &platform,
9394
bool gen_stdstring, bool gen_stdcomplex, bool is_c) : diag{diag},
95+
platform{platform},
9496
gen_stdstring{gen_stdstring}, gen_stdcomplex{gen_stdcomplex},
9597
is_c{is_c}, global_scope{nullptr} {}
9698

@@ -542,7 +544,20 @@ R"(#include <stdio.h>
542544

543545

544546
void visit_StringConstant(const ASR::StringConstant_t &x) {
545-
src = "\"" + std::string(x.m_s) + "\"";
547+
src = "\"";
548+
std::string s = x.m_s;
549+
for (size_t idx=0; idx < s.size(); idx++) {
550+
if (s[idx] == '\n') {
551+
src += "\\n";
552+
} else if (s[idx] == '\\') {
553+
src += "\\\\";
554+
} else if (s[idx] == '\"') {
555+
src += "\\\"";
556+
} else {
557+
src += s[idx];
558+
}
559+
}
560+
src += "\"";
546561
last_expr_precedence = 2;
547562
}
548563

src/libasr/codegen/asr_to_cpp.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ std::string format_type(const std::string &dims, const std::string &type,
6464
class ASRToCPPVisitor : public BaseCCPPVisitor<ASRToCPPVisitor>
6565
{
6666
public:
67-
ASRToCPPVisitor(diag::Diagnostics &diag) : BaseCCPPVisitor(diag,
68-
true, true, false) {}
67+
ASRToCPPVisitor(diag::Diagnostics &diag, Platform &platform)
68+
: BaseCCPPVisitor(diag, platform, true, true, false) {}
6969

7070
std::string convert_variable_decl(const ASR::Variable_t &v)
7171
{
@@ -419,10 +419,10 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
419419
};
420420

421421
Result<std::string> asr_to_cpp(Allocator &al, ASR::TranslationUnit_t &asr,
422-
diag::Diagnostics &diagnostics)
422+
diag::Diagnostics &diagnostics, Platform &platform)
423423
{
424424
pass_unused_functions(al, asr, true);
425-
ASRToCPPVisitor v(diagnostics);
425+
ASRToCPPVisitor v(diagnostics, platform);
426426
try {
427427
v.visit_asr((ASR::asr_t &)asr);
428428
} catch (const CodeGenError &e) {

src/libasr/codegen/asr_to_cpp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
#define LFORTRAN_ASR_TO_CPP_H
33

44
#include <libasr/asr.h>
5+
#include <libasr/utils.h>
56

67
namespace LFortran {
78

89
Result<std::string> asr_to_cpp(Allocator &al, ASR::TranslationUnit_t &asr,
9-
diag::Diagnostics &diagnostics);
10+
diag::Diagnostics &diagnostics, Platform &platform);
1011

1112
} // namespace LFortran
1213

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3263,6 +3263,26 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
32633263
tmp = builder->CreateFPToSI(tmp, type);
32643264
break;
32653265
};
3266+
case ASR::binopType::BitOr: {
3267+
tmp = builder->CreateOr(left_val, right_val);
3268+
break;
3269+
}
3270+
case ASR::binopType::BitAnd: {
3271+
tmp = builder->CreateAnd(left_val, right_val);
3272+
break;
3273+
}
3274+
case ASR::binopType::BitXor: {
3275+
tmp = builder->CreateXor(left_val, right_val);
3276+
break;
3277+
}
3278+
case ASR::binopType::BitLShift: {
3279+
tmp = builder->CreateShl(left_val, right_val);
3280+
break;
3281+
}
3282+
case ASR::binopType::BitRShift: {
3283+
tmp = builder->CreateAShr(left_val, right_val);
3284+
break;
3285+
}
32663286
}
32673287
}
32683288

@@ -3310,6 +3330,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
33103330
tmp = builder->CreateCall(fn_pow, {left_val, right_val});
33113331
break;
33123332
};
3333+
default: {
3334+
throw CodeGenError("Binary operator '" + ASRUtils::binop_to_str(x.m_op) + "' not supported",
3335+
x.base.base.loc);
3336+
}
33133337
}
33143338
}
33153339

@@ -3375,6 +3399,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
33753399
}
33763400
break;
33773401
};
3402+
default: {
3403+
throw CodeGenError("Binary operator '" + ASRUtils::binop_to_str(x.m_op) + "' not supported",
3404+
x.base.base.loc);
3405+
}
33783406
}
33793407
tmp = lfortran_complex_bin_op(left_val, right_val, fn_name, type);
33803408
}

src/libasr/codegen/asr_to_x86.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,9 @@ class ASRToX86Visitor : public ASR::BaseVisitor<ASRToX86Visitor>
328328
m_a.asm_div_r32(X86Reg::ecx);
329329
break;
330330
};
331-
case ASR::binopType::Pow: {
332-
throw CodeGenError("Pow not implemented yet.");
333-
break;
334-
};
331+
default: {
332+
throw CodeGenError("Binary operator '" + ASRUtils::binop_to_str(x.m_op) + "' not supported yet");
333+
}
335334
}
336335
}
337336

src/lpython/parser/semantics.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,24 @@ char* concat_string(Allocator &al, ast_t *a, char *b) {
423423
return LFortran::s2c(al, std::string(s) + std::string(b));
424424
}
425425

426+
char* unescape(Allocator &al, LFortran::Str &s) {
427+
std::string x;
428+
for (size_t idx=0; idx < s.size(); idx++) {
429+
if (s.p[idx] == '\\' && s.p[idx+1] == 'n') {
430+
x += "\n";
431+
idx++;
432+
} else {
433+
x += s.p[idx];
434+
}
435+
}
436+
return LFortran::s2c(al, x);
437+
}
438+
426439
#define SYMBOL(x, l) make_Name_t(p.m_a, l, \
427440
x.c_str(p.m_a), expr_contextType::Load)
428441
// `x.int_n` is of type BigInt but we store the int64_t directly in AST
429442
#define INTEGER(x, l) make_ConstantInt_t(p.m_a, l, x, nullptr)
430-
#define STRING1(x, l) make_ConstantStr_t(p.m_a, l, x.c_str(p.m_a), nullptr)
443+
#define STRING1(x, l) make_ConstantStr_t(p.m_a, l, unescape(p.m_a, x), nullptr)
431444
#define STRING2(x, y, l) make_ConstantStr_t(p.m_a, l, concat_string(p.m_a, x, y.c_str(p.m_a)), nullptr)
432445
#define STRING3(id, x, l) PREFIX_STRING(p.m_a, l, name2char(id), x.c_str(p.m_a))
433446
#define FLOAT(x, l) make_ConstantFloat_t(p.m_a, l, x, nullptr)

src/lpython/parser/tokenizer.re

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ bool lex_oct(const unsigned char *s, const unsigned char *e, uint64_t &u)
3131
bool lex_dec(const unsigned char *s, const unsigned char *e, uint64_t &u)
3232
{
3333
for (u = 0; s < e; ++s) {
34+
if (*s == '_') continue;
3435
if (!adddgt<10>(u, *s - 0x30u)) {
3536
return false;
3637
}
@@ -48,6 +49,7 @@ void lex_dec_int_large(Allocator &al, const unsigned char *s,
4849
return;
4950
}
5051
}
52+
// TODO: remove underscore from large ints
5153
const unsigned char *start = s;
5254
Str num;
5355
num.p = (char*)start;
@@ -262,14 +264,15 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost
262264
whitespace = [ \t\v\r]+;
263265
newline = "\n";
264266
digit = [0-9];
265-
oct_digit = "0"[oO][0-7]+;
266-
bin_digit = "0"[bB][01]+;
267-
hex_digit = "0"[xX][0-9a-fA-F]+;
267+
int_oct = "0"[oO][0-7]+;
268+
int_bin = "0"[bB][01]+;
269+
int_hex = "0"[xX][0-9a-fA-F]+;
270+
int_dec = digit+ (digit | "_" digit)*;
268271
char = [a-zA-Z_];
269272
name = char (char | digit)*;
270273
significand = (digit+ "." digit*) | ("." digit+);
271274
exp = [eE][-+]? digit+;
272-
integer = digit+ | oct_digit | bin_digit | hex_digit;
275+
integer = int_dec | int_oct | int_bin | int_hex;
273276
real = (significand exp?) | (digit+ exp);
274277
imag_number = (real | digit+)[jJ];
275278
string1 = '"' ('\\"'|[^"\x00])* '"';

0 commit comments

Comments
 (0)