From 589411f0de41fd351c8a0ec712cd7eacef98036d Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Mon, 18 Oct 2021 17:37:51 +0530 Subject: [PATCH] Added BOZ --- grammar/ASR.asdl | 3 +++ src/lfortran/asr_utils.h | 1 + src/lfortran/semantics/ast_common_visitor.h | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/grammar/ASR.asdl b/grammar/ASR.asdl index 51eaef0608..02280ba9f1 100644 --- a/grammar/ASR.asdl +++ b/grammar/ASR.asdl @@ -205,6 +205,7 @@ expr | ConstantComplex(float re, float im, ttype type) | ConstantLogical(bool value, ttype type) | ConstantString(string s, ttype type) + | BOZ(string s, boz boz_type, ttype? type) | Var(symbol v) | ArrayRef(symbol v, array_index* args, ttype type, expr? value) | DerivedRef(expr v, symbol m, ttype type, expr? value) @@ -242,6 +243,8 @@ strop = Concat cmpop = Eq | NotEq | Lt | LtE | Gt | GtE +boz = Binary | Hex | Octal + cast_kind = RealToInteger | IntegerToReal diff --git a/src/lfortran/asr_utils.h b/src/lfortran/asr_utils.h index 6fcd3efa06..8a4a724a07 100644 --- a/src/lfortran/asr_utils.h +++ b/src/lfortran/asr_utils.h @@ -97,6 +97,7 @@ static inline ASR::ttype_t* expr_type(const ASR::expr_t *f) case ASR::exprType::StrOp: { return ((ASR::StrOp_t*)f)->m_type; } case ASR::exprType::ImpliedDoLoop: { return ((ASR::ImpliedDoLoop_t*)f)->m_type; } case ASR::exprType::DerivedTypeConstructor: { return ((ASR::DerivedTypeConstructor_t*)f)->m_type; } + case ASR::exprType::BOZ: { return ((ASR::BOZ_t*)f)->m_type; } default : throw LFortranException("Not implemented"); } } diff --git a/src/lfortran/semantics/ast_common_visitor.h b/src/lfortran/semantics/ast_common_visitor.h index 161d405d02..ce4410c02f 100644 --- a/src/lfortran/semantics/ast_common_visitor.h +++ b/src/lfortran/semantics/ast_common_visitor.h @@ -1210,6 +1210,27 @@ class CommonVisitor : public AST::BaseVisitor { tmp = ASR::make_ConstantString_t(al, x.base.base.loc, x.m_s, type); } + void visit_BOZ(const AST::BOZ_t& x) { + LFortran::Str boz_content; + std::string s = x.m_s; + boz_content.from_str(al, s.substr(1)); + ASR::bozType boz_type; + if( s[0] == 'b' || s[0] == 'B' ) { + boz_type = ASR::bozType::Binary; + } else if( s[0] == 'z' || s[0] == 'Z' ) { + boz_type = ASR::bozType::Hex; + } else if( s[0] == 'o' || s[0] == 'O' ) { + boz_type = ASR::bozType::Octal; + } else { + throw SemanticError(R"""(Only 'b', 'o' and 'z' + are accepted as prefixes of + BOZ literal constants.)""", + x.base.base.loc); + } + tmp = ASR::make_BOZ_t(al, x.base.base.loc, boz_content.c_str(al), + boz_type, nullptr); + } + void visit_Num(const AST::Num_t &x) { int ikind = 4; if (x.m_kind) {