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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ tests/object/
**.exe

zlang-support/
zust-support/

**/.vscode/
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# zlang
# zust

A lightweight statically typed programming language that compiles to **x86_64 Linux assembly**, supporting custom data types, conditionals, arithmetic, and type-safe operations. The language was designed for educational and experimental purposes with an emphasis on low-level control and code generation.

Expand All @@ -21,7 +21,7 @@ A lightweight statically typed programming language that compiles to **x86_64 Li
## 📦 Project Structure

```
zlang/
zust/
├── include/
│ ├── common/
│ │ ├── Colors.hpp
Expand Down Expand Up @@ -95,8 +95,8 @@ zlang/

```bash
# Clone the repository
git clone https://github.com/your-username/zlang.git
cd zlang
git clone https://github.com/your-username/zust.git
cd zust

# Create build directory
mkdir build && cd build
Expand Down Expand Up @@ -177,9 +177,9 @@ gcc out.o -o out

---

## 🧠 Example Program (zlang)
## 🧠 Example Program (zust)

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

fn factorial(x: uint64_t) -> uint64_t{
Expand Down
41 changes: 18 additions & 23 deletions include/all.hpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
#pragma once

#include <assert.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <sstream>

#include "ast/ASTNode.hpp"
#include "codegen/Canaries.hpp"
#include "codegen/CodeGen.hpp"
#include "codegen/RegisterAllocator.hpp"
#include "common/Colors.hpp"
#include "common/Errors.hpp"
#include "common/Logging.hpp"
#include "common/StringUtils.hpp"

#include "support/CommandLine.hpp"
#include "support/File.hpp"

#include "ast/ASTNode.hpp"

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

#include "lexer/Lexer.hpp"

#include "support/CommandLine.hpp"
#include "support/File.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>

static NameMapper GLOBAL_NAME_MAPPER;
static zust::NameMapper GLOBAL_NAME_MAPPER;
4 changes: 2 additions & 2 deletions include/ast/ASTNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "parser/ScopeContext.hpp"

namespace zlang {
namespace zust {
enum class NodeType {
Program,
VariableDeclaration, // let x: int; or let x = 10;
Expand Down Expand Up @@ -73,4 +73,4 @@ namespace zlang {
ASTNode *getFunctionBody() const;
void print(std::ostream &out, int indent = 0) const;
};
} // namespace zlang
} // namespace zust
20 changes: 11 additions & 9 deletions include/codegen/Canaries.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#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);
}
};
namespace zust {
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);
}
};
}
4 changes: 2 additions & 2 deletions include/codegen/CodeGen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "codegen/RegisterAllocator.hpp"
#include "typechecker/TypeChecker.hpp"

namespace zlang {
namespace zust {
enum class TargetTriple {
X86_64_LINUX,
X86_64_WINDOWS,
Expand Down Expand Up @@ -247,4 +247,4 @@ namespace zlang {
CodeGenLLVM(std::ostream &outstream) : CodeGen(RegisterAllocator(), outstream) {};
void generate(std::unique_ptr<ASTNode> program) override;
};
} // namespace zlang
} // namespace zust
10 changes: 6 additions & 4 deletions include/codegen/RegisterAllocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <unordered_set>
#include <vector>

namespace zlang {
#include "support/CommandLine.hpp"

namespace zust {

// Tips for noobs: CALLER saved are saved by the caller, CALLEE saved are restored by the function/routine
// So CALLER Union CALLEE should be equal to the set of all the registers, so the current state will never be corrupted.
Expand Down Expand Up @@ -78,11 +80,11 @@ namespace zlang {
void markSpilled(const std::string &reg, const std::string &spillSlot);
bool isSpilled(const std::string &reg) const;
std::string spillSlotFor(const std::string &reg) const;
void unSpillXMM(const std::string &reg, zlang::CodegenOutputFormat format, std::ostream &out);
void unSpill(const std::string &reg, zlang::CodegenOutputFormat format, std::ostream &out);
void unSpillXMM(const std::string &reg, zust::CodegenOutputFormat format, std::ostream &out);
void unSpill(const std::string &reg, zust::CodegenOutputFormat format, std::ostream &out);
void touch(const std::string &reg);
void touchXMM(const std::string &reg);
void emitSpillRestore(const std::string &reg, const std::string &slot, bool isXMM, zlang::CodegenOutputFormat format, std::ostream &out);
void emitSpillRestore(const std::string &reg, const std::string &slot, bool isXMM, zust::CodegenOutputFormat format, std::ostream &out);

private:
RegisterAllocator(std::vector<std::string> regs, std::vector<std::string> XMMregs, std::vector<std::string> argumentRegs, std::vector<std::string> argumentXMMRegs);
Expand Down
4 changes: 2 additions & 2 deletions include/common/Colors.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
namespace zlang
namespace zust
{
namespace colors
{
Expand All @@ -13,4 +13,4 @@ namespace zlang
constexpr const char *WHITE = "\033[0;37m";
} // namespace colors

} // namespace zlang
} // namespace zust
4 changes: 2 additions & 2 deletions include/common/Errors.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include <string>

namespace zlang
namespace zust
{
enum class ErrorType
{
Expand All @@ -24,4 +24,4 @@ namespace zlang
operator bool() const { return type != ErrorType::None; }
};

} // namespace zlang
} // namespace zust
4 changes: 2 additions & 2 deletions include/common/Logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include <string>
#include "Errors.hpp"

namespace zlang
namespace zust
{

void logSystemError(const std::string &message);
void logError(const Error &err);
void logMessage(const std::string &message);

} // namespace zlang
} // namespace zust
4 changes: 2 additions & 2 deletions include/common/StringUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <string>
#include <vector>

namespace zlang
namespace zust
{

bool startsWith(const std::string &str, const std::string &prefix);
Expand Down Expand Up @@ -30,4 +30,4 @@ namespace zlang
return isAlpha(c) || isDigit(c) || c == '_';
}

} // namespace zlang
} // namespace zust
6 changes: 3 additions & 3 deletions include/lexer/Lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "../common/Errors.hpp"

namespace zlang {
namespace zust {
struct Token {
enum class Kind {
Let,
Expand Down Expand Up @@ -106,7 +106,7 @@ namespace zlang {
inline std::ostream &operator<<(std::ostream &os, const Token &token) {
return os << token.to_string();
}
using Error = zlang::Error;
using Error = zust::Error;
class Lexer {
public:
explicit Lexer(const std::string &source);
Expand All @@ -130,4 +130,4 @@ namespace zlang {
Token scanSymbol();
};

} // namespace zlang
} // namespace zust
14 changes: 6 additions & 8 deletions include/parser/NameMapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

#include <string>

class NameMapper
{
namespace zust {
class NameMapper {
public:
inline std::string mapVariable(const std::string &name, const std::string &scopeName)
{
inline std::string mapVariable(const std::string &name, const std::string &scopeName) {
std::string mangled = scopeName + "___" + name + "___v" + std::to_string(varCounter_++);
return mangled;
}

inline std::string mapFunction(const std::string &name, const std::string &scopeName)
{
inline std::string mapFunction(const std::string &name, const std::string &scopeName) {
std::string mangled = scopeName + "___" + name + "___f" + std::to_string(funcCounter_++);
return mangled;
}

inline std::string mapType(const std::string &name, const std::string &scopeName)
{
inline std::string mapType(const std::string &name, const std::string &scopeName) {
std::string mangled = scopeName + "___" + name + "___t" + std::to_string(typeCounter_++);
return mangled;
}
Expand All @@ -28,3 +25,4 @@ class NameMapper
size_t funcCounter_ = 0;
size_t typeCounter_ = 0;
};
}
2 changes: 1 addition & 1 deletion include/parser/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "common/Logging.hpp"
#include "parser/ScopeContext.hpp"

namespace zlang {
namespace zust {
class Parser {
public:
explicit Parser(Lexer &lexer);
Expand Down
6 changes: 4 additions & 2 deletions include/parser/ScopeContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <unordered_map>
#include <vector>

namespace zlang {
#include "support/CommandLine.hpp"

namespace zust {
struct VariableInfo {
std::string type;
};
Expand Down Expand Up @@ -171,4 +173,4 @@ namespace zlang {
}
};

} // namespace zlang
} // namespace zust
4 changes: 2 additions & 2 deletions include/support/CommandLine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <string>
#include <vector>

namespace zlang
namespace zust
{

enum class CodegenOutputFormat
Expand Down Expand Up @@ -54,4 +54,4 @@ namespace zlang
CodegenOutputFormat format = CodegenOutputFormat::Default;
};

} // namespace zlang
} // namespace zust
4 changes: 2 additions & 2 deletions include/support/File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string>
#include <optional>

namespace zlang
namespace zust
{

class File
Expand All @@ -14,4 +14,4 @@ namespace zlang
static std::optional<std::string> readAllText(const std::string &filepath);
};

} // namespace zlang
} // namespace zust
4 changes: 2 additions & 2 deletions include/typechecker/TypeChecker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// TODO: Check that all paths inside the function return appropriate value.

namespace zlang {
namespace zust {
static const std::set<std::string> numeric_types = {"integer", "size_t", "uint8_t", "uint16_t", "uint32_t", "uint64_t", "int8_t", "int16_t", "int32_t", "int64_t", "float", "double"};
static const std::set<std::string> integral_types = {"integer", "size_t", "uint8_t", "uint16_t", "uint32_t", "uint64_t", "int8_t", "int16_t", "int32_t", "int64_t"};

Expand Down Expand Up @@ -89,4 +89,4 @@ namespace zlang {
bool shouldCodegen_ = true;
};

} // namespace zlang
} // namespace zust
Loading