Giter Site home page Giter Site logo

itssamuelrowe / zen Goto Github PK

View Code? Open in Web Editor NEW
29.0 4.0 1.0 1.79 MB

Zen is a general purpose programming language designed to build simple, reliable and efficient programs.

License: Apache License 2.0

CMake 0.37% Shell 0.01% C 96.78% C++ 2.60% ANTLR 0.24% JavaScript 0.01%
zen programming-language virtual-machine garbage-collector compiler c zen-compiler jtk object-oriented-programming dynamic-typing

zen's Introduction

Zen

Zen is a general purpose programming language designed to build simple, reliable and efficient programs.

Zen is currently under development. Please contact the author, Samuel Rowe ([email protected]), to contribute.

Visit the official website to learn more about the language, tools, getting started, and more.

This repository contains the reference implementation of the Zen compiler. Please explore the following links to find other relevant resources.

Example 1

function factorial(n)
    return n <= 1 ? 1 : n * factorial(n - 1)

function main(...arguments)
    printf('Enter an integer: ')
    var n = scanInteger()
    var result = factorial(n)
    print('%d! = %d', n, result)

The above example generates the following output:

Enter an integer: 5
5! = 120

Example 2

function main(...arguments)
    printf('What is your name? ')
    var name = scanLine()
    if name.isBlank()
        print('The specified name is blank.')
    else if name == 'Samuel Rowe'
        print('Hey! That is my name, too!')
    else
        print('Hi, %s!', name)

The above example generates the following output:

What is your name? Samuel Rowe
Hey! That is my name, too!

Example 3

function main(...arguments)
    var names = [
        'Samuel Rowe',
        'Joel E. Rego',
        'Bill Gates',
        'Linus Trovalds',
        'Abraham Lincoln',
        'Isaac Newton',
        'Albert Einstein',
        'Richard Feynman',
        'Christopher Nolan',
        'Benedict Cumberbatch'
    ]
    var key = 'Marshall Mathers'
    var result = null
    for var i in range(0, names.size)
        if key == names[i]
            result = i
            break

    if result != null
        print('Found the key "%s" at index %d.', key, result)
    else
        print('Could not find the key "%s" in the list.', key)

The above example generates the following output:

Could not find the key "Marshall Mathers" in the list.

Installation

Before you install Zen, you need to install JTK and pkg-config on your system.

JTK is a library designed for writing applications and libraries in C. It provides core utilities such as collections, unit testing, I/O streams, threads and much more. You can find the latest version of JTK here.

Compiling Zen on Windows

For compiling Zen on Windows, you first need to install MSYS2 or WSL. The steps required to install MSYS2 or WSL are beyond the scope of this documentation. Although you can find tutorials on installing them on the Internet.

Please follow the steps given below if you are compiling on MSYS2. If you are using WSL to compile Zen, please follow the steps described under the Linux section.

  1. Extract the source code to any directory of your choice, for the sake of this documentation we will refer this location as 'C:\zen'.

  2. Change the directory to 'C:\zen'.

    cd 'C:/zen'
    
  3. Create a temporary directory for compiling the source code. For the sake of this documentation we will refer to this directory as 'build'.

    mkdir build
    
  4. Change directory to the newly created directory.

    cd build
    
  5. Invoke CMake to generate make files. In this documentation, we show the commands to generate GNU Makefiles. You can easily generate other makefiles similarly.

    cmake .. -G 'MSYS Makefiles'
    
  6. Invoke the GNU Make program. The command may be different on your system, if you have not setup an alias.

    make
    

    This should compile the compiler, virtual machine, and all the other executables. Archives and executable files should be produced in your current working directory.

    As of this version, a plethora of warnings are generated. We are working to eradicate these warnings.

Compiling Zen on Linux and WSL

  1. For compiling Zen on Linux or WSL, you need CMake and GNU Make (or any other make utility that CMake is compatible with).

  2. Extract the source code to any directory of your choice, for the sake of this documentation we will refer this location as '/mnt/g/zen'.

  3. Change the directory to '/mnt/g/zen'.

    cd '/mnt/g/zen'
    
  4. Create a temporary directory for compiling the source code. For the sake of this documentation we will refer to this directory as 'build'.

    mkdir build
    
  5. Change directory to the newly created directory.

    cd build
    
  6. Invoke CMake to generate make files. In this documentation, we show the commands to generate the default Makefiles, on our system GNU Makefiles is the default target. You can easily generate other makefiles by specifying the target make files using the -G flag.

    cmake ..
    
  7. Invoke the GNU Make program.

    make
    

    This should compile the compiler, virtual machine, and all the other executables. Archives and executable files should be produced in your current working directory.

    As of this version, a plethora of warnings are generated. We are working to eradicate these warnings.

Contributing

We welcome all contributors.

Zen was created with a vision to help programmers, like you and I, write code better. With your contributions, we can get there sooner. We appreciate your help!

To contribute, please contact the original author Samuel Rowe ([email protected]).

License

Copyright 2017-2020 Samuel Rowe

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

zen's People

Contributors

itssamuelrowe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

devanshusingla

zen's Issues

Integrate JIT compiler

The Zen virtual machine is pretty slow. A few optimizations are currently being implemented. However, to boost the performance further a JIT compiler should be integrated. The Eclipse OMR project implements a powerful JIT compiler which can be easily integrated.

Implement closures

Zen can encourage functional programming with closures. Given Zen does not treat functions as expressions, mostly due to the limitations imposed by indentation based blocks, local functions can be used to simulate closures.

Consider the following example which makes use of a closure.

// Test.zen

define makeCounter()
    var count = 1
    define next()
        count = count + 1
        return count
    return next

define main(...arguments)
    var next = makeCounter()
    var value = next()
    print(value)

In the background, the compiler treats the above example as if it were of the following form.

// Test.zen

class Test$Closure1 extends Closure
    
    var counter

    define new(counter0)
        counter = counter0
    
    define invoke()
        counter = counter + 1
        return counter

define makeCounter()
    var counter = 1
    var next = new Test$Closure1(counter)
    return next

define main(...arguments)
    var next = makeCounter()
    var value = next.invoke()
    print(value)
  • Are closures really necessary in Zen?
  • When a local variable or local field is treated as a function, the compiler
    translates it to reference.invoke(...). Initially, the target method can be invoked via invoke_dynamic. Once inheritance is implemented, such invocations can be done via the invoke_virtual instruction.
  • How is object.field() handled? The bootstrap method is a good place to load the
    target method.
bootstrap(...) {
    ...
    zen_Object_t* self = ...;
    zen_Function_t* function = ...;
    ...
    if (function == NULL) {
        zen_Field_t* field = ...;
        if (field != NULL) {
            zen_Class_t* class0 = zen_Object_getClass(field);
            function = zen_Class_getFunction(class0, "invoke", ...);
             ...
        }
    }
    return function;
}

Integrate Garbage Collector

The following Zen program albeit being simple leaves hundreds of garbage when executed on the virtual machine.

function main(...arguments)
    for var i in range(0, 10000):
         print(i)

This problem can be solved by integrating a garbage collector. Although Zen does not currently support multithreading, in the future it certainly will. Therefore, the garabage collector should acknowledge multithreading. The Eclipse OMR implements a variety of garbage collection algorithms with multithreading support.

Generate error when referencing uninitialized variables

Consider the following program:

function main(...arguments)
    var text
    if random() % 2 == 0
        text = 'even'
    print(text)

In the example shown above, text remains uninitialized when random() returns an odd number. Although this is not a problem for this simple example, but it definitely could cause bugs. Therefore, the compiler should report such errors.

Such errors can be found during static analysis in the resolution phase. The zen_Symbol_t structure can be extended to include flags. A flag which indicates the initialization status of a local variable can be used to detect the errors at zen_SymbolResolutionListener_onExitFunctionDeclaration().

Fix multiline and documentation comments

When a newline character appears immediately after a multiline or documentation comment, the lexer generates a newline token. This causes syntax errors.

The following code reproduces this bug:

class Example
    /**
     * Hello
     */
    function hello()
        ;

A possible fix is to update the lexer is consume newline characters that immediately follow these comments.

Remove the stale header and source files that were a part of the virtual machine.

Initially, the virtual machine and the compiler were part of the same repository. However, for maintainance purposes, the virtual machine was separated and moved to its own repository. The compiler, in particular the code generator, depends on structures and enumerations that are part of the virtual machine. As of now, there are two copies of these entities, one in the compiler repository and the other in the virtual machine repository. The files in the compilers repository are not updated anymore. Therefore, these "stale" header files (and possibly source files) should be removed. After which, building the compiler would require the header files of the virtual machine to be installed on the system.

Implement inheritance

Inheritance is a very important feature of object-oriented programming, a mechanism which allows one class to inherit the behaviors and attributes from another class. Inheritance allows you to create a hierarchy of classes. Thus, it has a direct impact on the design of programs written in Zen.

The design of inheritance should address the following questions.

  • Single inheritance or multiple inheritance?
  • In case of multiple inheritance, how is the diamond inheritance problem addressed?
  • What happens when two classes in an hierarchy have fields with the same names?
  • How are super constructors invoked?
  • What about abstract methods?
  • What about interfaces?

Report unreachable return statements

Consider the following program.

function isEven(n)
    if n % 2 == 0
         return true

If this function was invoked with an odd number, null is returned. However, the invoker may be expecting a boolean result. Therefore, the programmer must be reminded to return a value from a function for all cases if at least one return statement appears.

Fix compilation warnings generated when building Zen

For the most part, Zen seems to work fine. However, GCC generates a plethora of warnings when building it. Obviously, these warnings are insights into possible hidden bugs that I may have not encountered during development. I have disabled warnings in CMakeLists.txt file. It would be really helpful if someone could help fix the warnings.

Report unreachable statements because of return and throw statements

The compiler should analyze the function body to detect statements that are completely unreachable because of return and throw statements that cause a function to exit.

Consider the following example:

function main(...arguments)
    return 0
    print('Hello, world!')

In the above example, the print statement is never executed because main() is terminated before it can reach the print statement.

Extend the Import Statement to Declare Aliases

The syntax of the import statement can be extended to bind an entity name to a local name. It is sometimes desirable to bind entity names to a different local name, for example, to avoid name clashes. Without the use of aliases, the only way to refer to two entities with the same name in a given compilation unit is to use fully qualified names.

The proposed syntax change would add an optional as clause to the import statement, as follows:

import abc.Entity as EntityA
import xyz.Entity as EntityX

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.