/**
* lisp-compiler: Compiles LISP using LLVM.
* Copyright (C) 2022 Clyne Sullivan
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see .
*
* @file ast.hpp
* @brief Abstract Syntax Tree (AST) implementation.
*/
#ifndef AST_HPP
#define AST_HPP
#include
#include
#include
struct CompilerState;
namespace llvm {
struct Value;
}
namespace AST {
using Value = llvm::Value *;
struct Node
{
virtual ~Node() {};
virtual Value codegen(CompilerState&) = 0;
virtual std::string desc() {
return "Node";
}
};
struct Identifier : public Node
{
std::string name;
virtual Value codegen(CompilerState&) final;
virtual std::string desc() final {
return std::string("Identifier: ") + name;
}
};
struct Literal : public Node
{
enum Type {
Unknown,
Number,
} type = Unknown;
std::variant value;
virtual Value codegen(CompilerState&) final;
virtual std::string desc() final {
std::string str ("Literal: ");
switch (type) {
default:
case Unknown:
str += "unknown";
break;
case Number:
str += std::visit([](auto&& v) { return std::to_string(v); }, value);
break;
}
return str;
}
};
struct ProcedureCall : public Node
{
Node *callee;
std::vector operands;
virtual Value codegen(CompilerState&) final;
virtual std::string desc() final {
std::string str ("ProcedureCall: ");
str = str + "<" + (callee ? callee->desc() : "unknown") + ">";
str += '(';
if (!operands.empty()) {
for (const auto& op : operands) {
str = str + "<" + (op ? op->desc() : "unknown") + ">";
str += ", ";
}
str.pop_back();
str.pop_back();
}
str += ')';
return str;
}
};
struct LambdaExpression : public Node
{
std::vector operands;
std::vector body;
virtual Value codegen(CompilerState&) final;
virtual std::string desc() final {
return "LambdaExpression";
}
};
struct Conditional : public Node
{
Node *cond;
Node *iftrue;
Node *iffalse;
virtual Value codegen(CompilerState&) final;
virtual std::string desc() final {
return "Conditional";
}
};
struct Definition : public Node
{
Identifier *ident;
Node *value;
virtual Value codegen(CompilerState&) final;
virtual std::string desc() final {
return "Definition";
}
};
struct Assignment : public Node
{
Identifier *ident;
Node *value;
virtual Value codegen(CompilerState&) final;
virtual std::string desc() final {
return "Assignment";
}
};
struct DerivedExpression : public Node
{
std::string name;
virtual Value codegen(CompilerState&) final {
return nullptr;
}
virtual std::string desc() final {
return "DerivedExpression";
}
};
// MacroUse
// MacroBlock
// Includer
}
#endif // AST_HPP