Skip to content

Commit

Permalink
Unify reserved identifiers in a header (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
vla5924 authored May 11, 2024
1 parent 17c3692 commit 4a3fa1e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
25 changes: 25 additions & 0 deletions compiler/include/compiler/utils/language.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <string>

namespace utils {
namespace language {

// ----------------------------------------------------------------------------
// Reserved function names
// ----------------------------------------------------------------------------

inline const std::string funcMain = "main";
inline const std::string funcPrint = "print";
inline const std::string funcInput = "input";
inline const std::string funcRange = "range";
inline const std::string funcEnumerate = "enumerate";

// ----------------------------------------------------------------------------
// Reserved decorator names
// ----------------------------------------------------------------------------

inline const std::string atInline = "inline";

} // namespace language
} // namespace utils
6 changes: 5 additions & 1 deletion compiler/lib/backend/ast/optimizer/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
#include <algorithm>
#include <variant>

#include "compiler/utils/language.hpp"

#include "optimizer/optimizer_context.hpp"

using namespace ast;
using namespace optimizer;

namespace language = utils::language;

namespace {

bool isLiteral(const Node::Ptr &node) {
Expand Down Expand Up @@ -591,7 +595,7 @@ void removeEmptyBranchRoots(Node::Ptr node) {
void removeUnusedFunctions(SyntaxTree &tree) {
tree.root->children.remove_if([&functions = tree.functions](Node::Ptr node) {
const std::string &funcName = node->firstChild()->str();
return functions[funcName].useCount == 0 && funcName != "main";
return functions[funcName].useCount == 0 && funcName != language::funcMain;
});
}

Expand Down
11 changes: 7 additions & 4 deletions compiler/lib/backend/ast/semantizer/semantizer.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "semantizer/semantizer.hpp"

#include "compiler/ast/types.hpp"
#include "compiler/utils/language.hpp"

using namespace semantizer;
using namespace ast;

namespace language = utils::language;

static std::vector<TypeId> getFunctionArguments(const std::list<Node::Ptr> &functionArguments, VariablesTable &table) {
std::vector<TypeId> result;

Expand Down Expand Up @@ -131,7 +134,7 @@ static TypeId processUnknownTypeExpression(Node::Ptr &node, NodeType type, Seman

static TypeId processFunctionCall(Node::Ptr &node, SemantizerContext &ctx) {
const std::string &funcName = node->firstChild()->str();
bool isPrintFunction = (funcName == "print");
bool isPrintFunction = (funcName == language::funcPrint);
if (isPrintFunction) {
auto exprNode = node->lastChild()->lastChild();
auto child = exprNode->firstChild();
Expand All @@ -141,7 +144,7 @@ static TypeId processFunctionCall(Node::Ptr &node, SemantizerContext &ctx) {
}
auto funcIter = ctx.functions.find(funcName);
if (funcIter == ctx.functions.cend()) {
if (funcName == "input") {
if (funcName == language::funcInput) {
funcIter = ctx.functions.emplace(funcName, Function(BuiltInTypes::NoneType)).first;
TypeId type = ctx.findVariable(node->parent->firstChild());
Node::Ptr returnTypeNode = std::make_shared<Node>(NodeType::FunctionReturnType, node);
Expand Down Expand Up @@ -205,7 +208,7 @@ static void processExpression(Node::Ptr &node, TypeId var_type, SemantizerContex
if (child->type == NodeType::FunctionCall) {
const std::string &funcName = child->firstChild()->str();
TypeId retType = processFunctionCall(child, ctx);
if (retType != var_type && funcName != "input") {
if (retType != var_type && funcName != language::funcInput) {
pushTypeConversion(node, var_type);
}
continue;
Expand Down Expand Up @@ -339,7 +342,7 @@ static void parseFunctions(const std::list<Node::Ptr> &children, SemantizerConte
}
}

if (ctx.functions.find("main") == ctx.functions.end())
if (ctx.functions.find(language::funcMain) == ctx.functions.end())
ctx.errors.push<SemantizerError>("Function 'main' is not declared, although it has to be");
}

Expand Down
14 changes: 8 additions & 6 deletions compiler/lib/codegen/ast_to_llvmir/ir_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
#include <cassert>
#include <iostream>

#include "compiler/utils/language.hpp"

using namespace ast;
using namespace ir_generator;

namespace language = utils::language;

namespace {

bool lhsRequiresPtr(BinaryOperation op) {
Expand All @@ -23,8 +27,6 @@ bool isLLVMPointer(llvm::Value *value) {
return value->getType()->isPointerTy();
}

constexpr const char *const PRINT_FUNCTION_NAME = "print";
constexpr const char *const INPUT_FUNCTION_NAME = "input";
constexpr const char *const PRINTF_FUNCTION_NAME = "printf";
constexpr const char *const SCANF_FUNCTION_NAME = "scanf";

Expand Down Expand Up @@ -91,8 +93,8 @@ static const std::unordered_map<std::string, std::string> placeholders = {
};

const std::unordered_map<std::string, IRGenerator::NodeVisitor> IRGenerator::builtInFunctions = {
{PRINT_FUNCTION_NAME, &IRGenerator::visitPrintFunctionCall},
{INPUT_FUNCTION_NAME, &IRGenerator::visitInputFunctionCall},
{language::funcPrint, &IRGenerator::visitPrintFunctionCall},
{language::funcInput, &IRGenerator::visitInputFunctionCall},
};

IRGenerator::IRGenerator(const std::string &moduleName, bool emitDebugInfo)
Expand Down Expand Up @@ -405,7 +407,7 @@ llvm::Value *IRGenerator::visitVariableName(Node *node) {
}

llvm::Value *IRGenerator::visitPrintFunctionCall(Node *node) {
assert(node && node->type == NodeType::FunctionCall && node->firstChild()->str() == PRINT_FUNCTION_NAME);
assert(node && node->type == NodeType::FunctionCall && node->firstChild()->str() == language::funcPrint);

auto argsNode = node->lastChild();
assert(argsNode->children.size() == 1u); // print requires only one argument
Expand All @@ -427,7 +429,7 @@ llvm::Value *IRGenerator::visitPrintFunctionCall(Node *node) {
}

llvm::Value *IRGenerator::visitInputFunctionCall(Node *node) {
assert(node && node->type == NodeType::FunctionCall && node->firstChild()->str() == INPUT_FUNCTION_NAME);
assert(node && node->type == NodeType::FunctionCall && node->firstChild()->str() == language::funcInput);

TypeId returnType = node->lastChild()->typeId();
llvm::Type *llvmType = createLLVMType(returnType);
Expand Down
13 changes: 10 additions & 3 deletions compiler/lib/frontend/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "compiler/optree/types.hpp"
#include "compiler/optree/value.hpp"
#include "compiler/utils/helpers.hpp"
#include "compiler/utils/language.hpp"
#include "compiler/utils/source_ref.hpp"

#include "converter/converter_context.hpp"
Expand All @@ -31,6 +32,10 @@ using ast::Node;
using ast::NodeType;
using ast::SyntaxTree;

namespace language = utils::language;

namespace {

Type::Ptr convertType(ast::TypeId typeId) {
switch (typeId) {
case ast::IntType:
Expand Down Expand Up @@ -83,7 +88,7 @@ bool isRhsInAssignment(const Node::Ptr &node) {
}

bool isFunctionCallInputNode(const Node::Ptr &node) {
return node->type == NodeType::FunctionCall && node->firstChild()->str() == "input";
return node->type == NodeType::FunctionCall && node->firstChild()->str() == language::funcInput;
}

bool isAssignment(ast::BinaryOperation binOp) {
Expand Down Expand Up @@ -332,7 +337,7 @@ Value::Ptr visitVariableName(const Node::Ptr &node, ConverterContext &ctx) {

Value::Ptr visitFunctionCall(const Node::Ptr &node, ConverterContext &ctx) {
const std::string &name = node->firstChild()->str();
if (name == "print") {
if (name == language::funcPrint) {
if (node->parent->type != NodeType::Expression) {
ctx.pushError(node, "print() statement cannot be within an expression context");
throw ctx.errors;
Expand All @@ -343,7 +348,7 @@ Value::Ptr visitFunctionCall(const Node::Ptr &node, ConverterContext &ctx) {
ctx.insert<PrintOp>(node->ref, arguments);
return {};
}
if (name == "input") {
if (name == language::funcInput) {
ctx.pushError(node, "input() statement must be a right-handed operand of an isolated assignment expression");
throw ctx.errors;
}
Expand Down Expand Up @@ -412,6 +417,8 @@ Value::Ptr visitNode(const Node::Ptr &node, ConverterContext &ctx) {
}
}

} // namespace

Program Converter::process(const SyntaxTree &syntaxTree) {
ConverterContext ctx;
processNode(syntaxTree.root, ctx);
Expand Down

0 comments on commit 4a3fa1e

Please sign in to comment.