Giter VIP home page Giter VIP logo

zigcompiler's Introduction

Tale, the programming language

A work-in-progress programming language interpreter/compiler written in Zig.

Testing using zig run .\src\main.zig -freference-trace -- ./examples/fizzbuzz.tale, but I'll figure out how to use Zig's build system sooner or later.

I've been inspired by Crafting Interpreters and Let's Build a Simple Interpreter.

TODO

  • Implement a lexer
  • Implement a parser
  • Implement an interpreter
    • Tree-walking interpreter
    • Bytecode interpreter
  • Implement a compiler

The language

The language, Tale, is not very well-defined yet -- I'm mostly experimenting with different ideas. However, here are the characteristics I'm aiming for:

  • Dynamic typing (for now)
  • First-class functions
  • C-derived syntax
  • Automatic reference counting
  • Simple primatives: Booleans, doubles, strings, functions, and null (I'm not sure if arrays or classes should go here)
  • Simple operators (no overloading):
    • Arithmetic: +, -, *, /, % (modulo has the same precedence as multiplication and division)
    • Comparison: ==, !=, <, >, <=, >=
    • Logical: !, &&, ||
      • Truthiness: booleans are obvious, strings are truthy if they are not empty, numbers are truthy if they are not 0, and null is falsy
    • Bitwise: &, |, ^
      • We take the Javascript approach to bitwise operators: convert their operands to 32-bit signed integers before performing the operation
    • Grouping: ()
  • Simple control-flow constructs:
    • if statements, along with else if and else
    • while loops
    • for loops (only for iterating over arrays -- for (let x . array) { ... }) (I'm not sure if I should add a more general for loop or even condense both loops into one)
    • break and continue statements
    • return statements
  • Block scoping: {}
  • Comments: // and /* */
  • Variable declaration: let x = 5;, let x; (uninitialized -- value is null)
  • Assignment: x = 5;
  • Functions:
    • Declaration: let add = function(x, y) { return x + y; } (This is a value that must be assigned to a variable)
      • If no value is returned, the function returns null
      • Functions hold a reference to the environment in which they were created -- commonly referred to as a closure
    • Calling: add(5, 3); (add is a variable containing a function)
  • Classes
    • Declaration: let Point = class { constructor(x, y) { this.x = x; this.y = y; } }
      • The constructor method is called when the class is instantiated
    • Instantiation: let p = Point(5, 3);
    • Field access: p.x, p.z = 5; (creates a new field)
    • Single inheritance: let Point3D = class extending Point { constructor(x, y, z) { super.constructor(x, y); this.z = z; } }
      • Constructors are inherited just like any other method
    • Method access: p.method() (Just another function)
    • Static:
      • Methods: let Point = class { static method() { return 5; } }
        • Access: Point.method() (No this reference)
      • Fields: let Point = class { static x = 5; }
        • Access: Point.x
  • Extremely simple standard library, globally accessible as Std (for now):
    • print function: Std.print("Hello, world!");
    • (From Crafting Interpreters) clock function: std.print(Std.clock()); (Returns the time in seconds since the program started running)
    • panic function: Std.panic("Something went wrong!"); (Throws an irrecoverable error)
    • assert function: Std.assert(5 == 5); (Panics if the condition is false)

Considerations for the future

  • Static typing
  • How should imports work (and how should we change the standard library so it's not just a global object)?
  • How should we handle errors?
  • Should null be a type?
  • Should statements be expressions? (E.g. let x = if (true) { 5 } else { 3 })

Development

Random

When developing on Windows, you'll need to paste this in PowerShell because it doesn't recognize UTF-8 by default:

$OutputEncoding = [Console]::InputEncoding = [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding

I might eventually find the proper way to do this, but for now, this works.

FAQ (Questions to myself)

Where does the name come from?

A random noun generator. I needed a name and I couldn't think of one ¯\_(ツ)_/¯

Why object-oriented?

It's the most familiar paradigm to me, and since I want to eventually self-host this language, I want to make it as easy as possible to work with.
I also personally think it can be a very powerful style when used correctly, and it's not that difficult to implement.
Another part of it is that I want to learn how object-oriented features are implemented.

Why did you make this?

I'm making this language because I wanted to learn Zig and I'm interested in compiler design.

What's the goal of this project?

To make a programming language that is relatively fast, easy-to-use (ish), and compiles to various targets.

zigcompiler's People

Contributors

glitch752 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.