summaryrefslogtreecommitdiff
path: root/willow/tools/willowc/include
diff options
context:
space:
mode:
authorStefan Weigl-Bosker <stefan@s00.xyz>2026-02-03 14:59:53 -0500
committerGitHub <noreply@github.com>2026-02-03 14:59:53 -0500
commitadd95b14f74e6dbe04a6efe98ff0f20424930b73 (patch)
tree13ce413ee4190a4c8f8743c7740aaa8d04353f14 /willow/tools/willowc/include
parentc5b2905c5a64433f8519531a77d3acc42d881f17 (diff)
downloadcompiler-add95b14f74e6dbe04a6efe98ff0f20424930b73.tar.gz
[willow]: initial frontend work, unit tests (#8)dev/stefan
Diffstat (limited to 'willow/tools/willowc/include')
-rw-r--r--willow/tools/willowc/include/ast.hpp62
-rw-r--r--willow/tools/willowc/include/expr.hpp4
-rw-r--r--willow/tools/willowc/include/parser.hpp33
-rw-r--r--willow/tools/willowc/include/sourcemanager.hpp26
-rw-r--r--willow/tools/willowc/include/tokenizer.hpp72
5 files changed, 197 insertions, 0 deletions
diff --git a/willow/tools/willowc/include/ast.hpp b/willow/tools/willowc/include/ast.hpp
new file mode 100644
index 0000000..8c59067
--- /dev/null
+++ b/willow/tools/willowc/include/ast.hpp
@@ -0,0 +1,62 @@
+#ifndef WILLOWC_INCLUDE_AST_HPP
+#define WILLOWC_INCLUDE_AST_HPP
+
+#include <cstdint>
+#include <memory>
+#include <vector>
+
+#include <willow/IR/Instructions.h>
+#include <willow/IR/Types.h>
+
+#include <tokenizer.hpp>
+
+namespace willowc {
+
+using Opcode = willow::Instruction::Opcode;
+using TokenIndex = std::size_t;
+
+// this is like willow::ValueKind, but treats groups all ssa values into 'Value'
+// (because they can't be differentiated by syntax alone)
+enum class ExprKind { Constant, BasicBlock, Function, Value };
+
+struct ExprAST {
+ ExprKind kind;
+
+ std::string name;
+ // token??
+};
+
+struct InstAST {
+ Opcode op;
+ std::string name;
+
+ std::vector<std::unique_ptr<ExprAST>> args;
+};
+
+struct BlockAST {
+ std::string label;
+ std::vector<std::unique_ptr<InstAST>> body;
+};
+
+struct ParameterAST {
+ std::string name;
+ willow::Type type;
+};
+
+struct FunctionDeclAST {
+ std::string name;
+ std::vector<std::unique_ptr<ParameterAST>> parameters;
+ std::string returntype;
+
+ std::vector<std::unique_ptr<BlockAST>> body;
+ // TODO: movable symbol table
+};
+
+struct ModuleAST {
+ std::vector<std::unique_ptr<FunctionDeclAST>> Functions;
+ // TODO: imports, symbol table
+};
+
+}; // namespace willowc
+
+#endif // WILLOWC_INCLUDE_AST_HPP
diff --git a/willow/tools/willowc/include/expr.hpp b/willow/tools/willowc/include/expr.hpp
new file mode 100644
index 0000000..15d2985
--- /dev/null
+++ b/willow/tools/willowc/include/expr.hpp
@@ -0,0 +1,4 @@
+#ifndef WILLOWC_INCLUDE_EXPR_HPP
+#define WILLOWC_INCLUDE_EXPR_HPP
+
+#endif // WILLOWC_INCLUDE_EXPR_HPP
diff --git a/willow/tools/willowc/include/parser.hpp b/willow/tools/willowc/include/parser.hpp
new file mode 100644
index 0000000..825dfdd
--- /dev/null
+++ b/willow/tools/willowc/include/parser.hpp
@@ -0,0 +1,33 @@
+#ifndef WILLOWC_INCLUDE_PARSER_HPP
+#define WILLOWC_INCLUDE_PARSER_HPP
+
+#include <tokenizer.hpp>
+#include <ast.hpp>
+
+#include <optional>
+#include <memory>
+#include <vector>
+
+namespace willowc {
+
+class Parser {
+ std::string_view buf;
+
+ std::vector<TokenKind> kinds;
+ std::vector<std::size_t> starts;
+ Tokenizer tokenizer;
+
+ std::size_t pos;
+
+public:
+ Parser(std::string_view buf) : buf(buf), tokenizer(buf) {}
+
+ std::optional<std::unique_ptr<ModuleAST>> parse();
+
+ TokenKind kind() const { return kinds[pos]; }
+ std::size_t start() const { return starts[pos]; }
+};
+
+} // namespace willowc
+
+#endif // WILLOWC_INCLUDE_PARSER_HPP
diff --git a/willow/tools/willowc/include/sourcemanager.hpp b/willow/tools/willowc/include/sourcemanager.hpp
new file mode 100644
index 0000000..a526e48
--- /dev/null
+++ b/willow/tools/willowc/include/sourcemanager.hpp
@@ -0,0 +1,26 @@
+#ifndef WILLOWC_INCLUDE_SOURCEMANAGER_HPP
+#define WILLOWC_INCLUDE_SOURCEMANAGER_HPP
+
+#include <filesystem>
+#include <memory>
+#include <string>
+#include <vector>
+
+namespace willowc {
+
+using FileID = std::uint32_t;
+
+class SourceManager {
+struct File {
+ std::string path;
+ std::unique_ptr<char[]> buf;
+};
+public:
+ std::optional<FileID> addFile(std::string_view path);
+private:
+ std::vector<File> file_table;
+};
+
+} // namespace willowc
+
+#endif // WILLOWC_INCLUDE_SOURCEMANAGER_HPP
diff --git a/willow/tools/willowc/include/tokenizer.hpp b/willow/tools/willowc/include/tokenizer.hpp
new file mode 100644
index 0000000..3de9d32
--- /dev/null
+++ b/willow/tools/willowc/include/tokenizer.hpp
@@ -0,0 +1,72 @@
+#ifndef WILLOWC_INCLUDE_TOKENIZER_HPP
+#define WILLOWC_INCLUDE_TOKENIZER_HPP
+
+#include <willow/IR/Location.h>
+
+namespace willowc {
+
+enum class TokenKind {
+ Function,
+ Variable,
+ Constant,
+ Type,
+ Label,
+ Inst,
+
+ Comma,
+ Semicolon,
+ LParen,
+ RParen,
+ LCurly,
+ RCurly,
+ Equals,
+ RArrow,
+ Comment,
+
+ FuncKW,
+ Eof,
+ Invalid,
+};
+
+struct Token {
+ std::size_t start, end;
+ TokenKind kind;
+};
+
+class Tokenizer {
+ std::string_view buf;
+ std::size_t offset;
+
+ void skip(std::size_t idx = 1) { offset += idx; }
+
+ char eat(std::size_t num = 1) {
+ if (offset >= buf.length())
+ return '\0';
+
+ char c = buf[offset];
+ offset += num;
+ return c;
+ }
+
+ char peek(std::size_t idx = 0) {
+ if (offset + idx >= buf.length())
+ return '\0';
+
+ return buf[offset + idx];
+ }
+
+ bool scan_id(bool accept_digits);
+ bool scan_dec();
+ bool scan_hex();
+ bool scan_constant();
+public:
+ explicit Tokenizer(std::string_view buf, std::size_t offset = 0)
+ : buf{buf}, offset{offset} {}
+
+ Token scan();
+ void seek(uint64_t offset);
+};
+
+} // namespace willowc
+
+#endif // WILLOWC_INCLUDE_TOKENIZER_HPP