Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ tests/object/

**.obj
**.exe

zlang-support/
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,8 @@
"xtree": "cpp",
"xutility": "cpp"
},
"C_Cpp.default.compilerPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.44.35207\\bin\\Hostx86\\x64\\cl.exe"
"C_Cpp.formatting": "clangFormat",
"C_Cpp.clang_format_style": "{ BasedOnStyle: Google, IndentWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false}",
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false}",
"C_Cpp.clang_format_path": "/usr/bin/clang-format",
}
17 changes: 17 additions & 0 deletions examples/functions.zz
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

extern fn printf(fmt: string, ...) -> int32_t;

let a: int64_t = 10;
let f: float = 20.5;
let u: uint8_t = 10;

fn print_int(x: int64_t) -> none {
printf("%d\n", x);
}

fn main() {
if (a > 100) {
let res: int64_t = a * 2;
print_int(res);
}
}
7 changes: 6 additions & 1 deletion include/all.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "common/Colors.hpp"
#include "common/Errors.hpp"
#include "common/Logging.hpp"
Expand All @@ -11,18 +12,22 @@

#include "parser/Parser.hpp"
#include "parser/ScopeContext.hpp"
#include "parser/NameMapper.hpp"

#include "lexer/Lexer.hpp"

#include "typechecker/TypeChecker.hpp"

#include "codegen/CodeGen.hpp"
#include "codegen/RegisterAllocator.hpp"
#include "codegen/Canaries.hpp"

#include <sstream>
#include <map>
#include <iostream>
#include <memory>
#include <fstream>
#include <assert.h>
#include <iomanip>
#include <iomanip>

static NameMapper GLOBAL_NAME_MAPPER;
42 changes: 26 additions & 16 deletions include/ast/ASTNode.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#pragma once

#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <memory>
#include <iostream>

#include "parser/ScopeContext.hpp"

namespace zlang
{
enum class NodeType
{
namespace zlang {
enum class NodeType {
Program,
VariableDeclaration, // let x: int; or let x = 10;
VariableReassignment, // x = 42;
VariableAccess, // just x
IntegerLiteral, // 10, 42
VariableDeclaration, // let x: int; or let x = 10;
VariableReassignment, // x = 42;
VariableAccess, // just x
IntegerLiteral, // 10, 42
FloatLiteral,
StringLiteral,
BooleanLiteral,
Expand All @@ -24,26 +23,30 @@ namespace zlang
BinaryOp,
UnaryOp,
Symbol,
Function,
ExternFunction,
FunctionParameter,
FunctionParameterList,
FunctionReturnType,
ReturnStatement,
FunctionCall,
FunctionCallArgumentList
};

class ASTNode
{
class ASTNode {
public:
NodeType type;
std::string value;
std::vector<std::unique_ptr<ASTNode>> children;
std::shared_ptr<ScopeContext> scope;

ASTNode() = default;
ASTNode(NodeType t, const std::string &val = "", std::shared_ptr<ScopeContext> sc = nullptr)
: type(t), value(val), scope(sc) {}

static std::unique_ptr<ASTNode> makeProgramNode(const std::shared_ptr<ScopeContext> scope);
static std::optional<std::unique_ptr<ASTNode>> makeVariableDeclarationNode(
const std::string &name,
std::unique_ptr<ASTNode> typeAnnotation,
std::unique_ptr<ASTNode> initializer, const std::shared_ptr<ScopeContext> scope);

static std::unique_ptr<ASTNode> makeVariableReassignmentNode(
const std::string &name,
std::unique_ptr<ASTNode> expr, const std::shared_ptr<ScopeContext> scope);
Expand All @@ -58,9 +61,16 @@ namespace zlang
static std::unique_ptr<ASTNode> makeIfStatement(std::unique_ptr<ASTNode> condition, std::unique_ptr<ASTNode> program, const std::shared_ptr<ScopeContext> scope);
static std::unique_ptr<ASTNode> makeElseIfStatement(std::unique_ptr<ASTNode> condition, std::unique_ptr<ASTNode> program, const std::shared_ptr<ScopeContext> scope);
static std::unique_ptr<ASTNode> makeElseStatement(std::unique_ptr<ASTNode> program, const std::shared_ptr<ScopeContext> scope);
static std::unique_ptr<ASTNode> makeExternFunctionDeclaration(std::string name, const std::shared_ptr<ScopeContext> scope, std::vector<ParamInfo> params, std::string returnType, bool isVariadic);
static std::unique_ptr<ASTNode> makeFunctionDeclaration(std::string name, const std::shared_ptr<ScopeContext> scope, std::vector<ParamInfo> params, std::string returnType, std::unique_ptr<ASTNode> body, bool isVariadic);
static std::unique_ptr<ASTNode> makeFunctionCall(std::string name, std::vector<std::unique_ptr<ASTNode>> arguments, const std::shared_ptr<ScopeContext> scope);
static std::unique_ptr<ASTNode> makeFunctionParameterList(const std::vector<ParamInfo> params, const std::shared_ptr<ScopeContext> scope);
void addChild(std::unique_ptr<ASTNode> child);
void setElseBranch(std::unique_ptr<ASTNode> elseNode);
ASTNode *getElseBranch() const;
ASTNode *getFunctionParamList() const;
ASTNode *getFunctionParamReturnType() const;
ASTNode *getFunctionBody() const;
void print(std::ostream &out, int indent = 0) const;
};
}
} // namespace zlang
12 changes: 12 additions & 0 deletions include/codegen/Canaries.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <cstdint>
#include <random>

class CanaryGenerator {
public:
static std::uint64_t generate() {
std::random_device rd; // Cryptographically secure random source
std::mt19937_64 gen(rd());
std::uniform_int_distribution<std::uint64_t> dis;
return dis(gen);
}
};
Loading