Skip to content

Commit

Permalink
Added ValueTranslator cpp file, starting to move things over there.
Browse files Browse the repository at this point in the history
  • Loading branch information
azreika committed Oct 16, 2020
1 parent bd8f021 commit 34d3d11
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Expand Up @@ -201,6 +201,7 @@ souffle_sources = \
ast2ram/ProvenanceClauseTranslator.h \
ast2ram/ValueIndex.cpp \
ast2ram/ValueIndex.h \
ast2ram/ValueTranslator.cpp \
ast2ram/ValueTranslator.h \
interpreter/Context.h \
interpreter/Engine.cpp \
Expand Down
49 changes: 49 additions & 0 deletions src/ast2ram/ValueTranslator.cpp
@@ -0,0 +1,49 @@
/*
* Souffle - A Datalog Compiler
* Copyright (c) 2020 The Souffle Developers. All rights reserved
* Licensed under the Universal Permissive License v 1.0 as shown at:
* - https://opensource.org/licenses/UPL
* - <souffle root>/licenses/SOUFFLE-UPL.txt
*/

/************************************************************************
*
* @file ValueTranslator.cpp
*
***********************************************************************/

#include "ast2ram/ValueTranslator.h"
#include "ast/NumericConstant.h"
#include "ast2ram/AstToRamTranslator.h"
#include "ast2ram/ValueIndex.h"
#include "ram/FloatConstant.h"
#include "ram/SignedConstant.h"
#include "ram/UndefValue.h"
#include "ram/UnsignedConstant.h"

namespace souffle::ast2ram {

Own<ram::Expression> ValueTranslator::visitVariable(const ast::Variable& var) {
assert(index.isDefined(var) && "variable not grounded");
return translator.makeRamTupleElement(index.getDefinitionPoint(var));
}

Own<ram::Expression> ValueTranslator::visitUnnamedVariable(const ast::UnnamedVariable&) {
return mk<ram::UndefValue>();
}
Own<ram::Expression> ValueTranslator::visitNumericConstant(const ast::NumericConstant& c) {
assert(c.getType().has_value() && "At this points all constants should have type.");

switch (*c.getType()) {
case ast::NumericConstant::Type::Int:
return mk<ram::SignedConstant>(RamSignedFromString(c.getConstant(), nullptr, 0));
case ast::NumericConstant::Type::Uint:
return mk<ram::UnsignedConstant>(RamUnsignedFromString(c.getConstant(), nullptr, 0));
case ast::NumericConstant::Type::Float:
return mk<ram::FloatConstant>(RamFloatFromString(c.getConstant()));
}

fatal("unexpected numeric constant type");
}

} // namespace souffle::ast2ram
37 changes: 9 additions & 28 deletions src/ast2ram/ValueTranslator.h
Expand Up @@ -10,13 +10,10 @@
*
* @file ValueTranslator.h
*
* Value translator.
*
***********************************************************************/

#pragma once

#include "ast/NumericConstant.h"
#include "ast/UnnamedVariable.h"
#include "ast/Variable.h"
#include "ast/analysis/Functor.h"
Expand All @@ -36,7 +33,6 @@
#include "ram/Expression.h"
#include "ram/Extend.h"
#include "ram/Filter.h"
#include "ram/FloatConstant.h"
#include "ram/IO.h"
#include "ram/IntrinsicOperator.h"
#include "ram/LogRelationTimer.h"
Expand All @@ -61,42 +57,27 @@
#include "ram/Swap.h"
#include "ram/TranslationUnit.h"
#include "ram/TupleElement.h"
#include "ram/UndefValue.h"
#include "ram/UnsignedConstant.h"
#include "ram/UserDefinedOperator.h"
#include "ram/utility/Utils.h"

namespace souffle::ast {
class Variable;
class UnnamedVariable;
class NumericConstant;
} // namespace souffle::ast

namespace souffle::ast2ram {

class ValueTranslator : public ast::Visitor<Own<ram::Expression>> {
public:
ValueTranslator(AstToRamTranslator& translator, const ValueIndex& index, SymbolTable& symTab)
: translator(translator), index(index), symTab(symTab) {}

Own<ram::Expression> visitVariable(const ast::Variable& var) override {
assert(index.isDefined(var) && "variable not grounded");
return translator.makeRamTupleElement(index.getDefinitionPoint(var));
}

Own<ram::Expression> visitUnnamedVariable(const ast::UnnamedVariable&) override {
return mk<ram::UndefValue>();
}
Own<ram::Expression> visitVariable(const ast::Variable& var) override;

Own<ram::Expression> visitNumericConstant(const ast::NumericConstant& c) override {
assert(c.getType().has_value() && "At this points all constants should have type.");

switch (*c.getType()) {
case ast::NumericConstant::Type::Int:
return mk<ram::SignedConstant>(RamSignedFromString(c.getConstant(), nullptr, 0));
case ast::NumericConstant::Type::Uint:
return mk<ram::UnsignedConstant>(RamUnsignedFromString(c.getConstant(), nullptr, 0));
case ast::NumericConstant::Type::Float:
return mk<ram::FloatConstant>(RamFloatFromString(c.getConstant()));
}

fatal("unexpected numeric constant type");
}
Own<ram::Expression> visitUnnamedVariable(const ast::UnnamedVariable&) override;

Own<ram::Expression> visitNumericConstant(const ast::NumericConstant& c);
Own<ram::Expression> visitStringConstant(const ast::StringConstant& c) override {
return mk<ram::SignedConstant>(symTab.lookup(c.getConstant()));
}
Expand Down

0 comments on commit 34d3d11

Please sign in to comment.