GithubHelp home page GithubHelp logo

ahnan4arch / halang Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vincentdchan/halang

0.0 2.0 0.0 220 KB

A toy language implementation. Including lexer, parser, code generation and interpreter.

License: BSD 3-Clause "New" or "Revised" License

C++ 99.05% C 0.95%

halang's Introduction

Ha Language 哈语言

A toy dynamic language implementation.

  • ✔ Lexer
  • ✔ Parser
  • ✔ Code Generation
  • ✔ Stack VM
  • ✔ Simple mark-sweep GC
  • ✔ Function and Closure
  • ◦ Class
  • ◦ Yield
  • ◦ ...

Usage

By default, the program read the file named "source.txt" in the same location with the program, but you can pass the filename as the parameter to the program.

Language

This language is similar to JavaScript, but it has differences because this project is not completely finished.

String

In HaLang, string is a immutable built-in object. It's described within "".

var a = "Hello World!" // define a variable name "a", and it's value is "Hello World!"
print(a)

// concat
var b = "Hello " + "world!"	// result: Hello world!
// whilch is equal to "Hello".__add__("world")
print(a)

Expression

Similar to JavaScript.

2 * (3 + 4) // 14

Assignment

You can define a variable by using var statement.

and you can do assign operation everywhere.

var a = 1 + 1  // a == 2
var b = a + 1  // b == 3

a = a + 1 // a ==3

Branches

You can use the if and else expression. But there is a little differences from JavaScript.

var a = 100
if (a > 0)
	a = -a
	
if (a > 0) { print(-a) } else { print(a + 100) } // Correct! output: -100

This situation is due to the implementation of parser. I just implemented it in a very early stage. There will be a enhancement in the future to improve this situation.

Loop

You can use while expression to loop. The usage of while are similar to if statement.

var a = 0
while (a <= 50)
{
  a = a + 1
  print(a)
}

// for statement
for (var i = 0; i < 10; i++) {
  print(i);
}
// which is equal to
function _tmp() {
  	var i = 0;
  	while(i < 10) {
  		print(i);
    	i++;
	}
}
_tmp();

Function

# 2016/8/4 Updates

Halang supports function now.

func hello(a)
{
	if (a > 3)
		return a + 1
	else
		return a
}

var a = 0
while (a < 30)
{
	hello(a)
	a = a + 1
}

It's quite easy to understand.

# 2016/8/6 Updates

HaLang support recursive calling function now.

example:

func hello(a)
{
	if (a == 0)
		return 1
	return a*hello(a-1)
}

And also, Halang supports closure.

func hello(a)
{
	func ho(b)
	{
		a = a + b
		return a;
	}
	return ho;
}

var a = hello(5);	// type int
a(1);			// 6
a(1);			// 7
a(8);			// 15

Closure

func A(a) {
	func B(b) {
    	func C(c) {
          	return a + b + c;
		}
      	return C;
	}
  	return B;
}
var funcB = A("a");
var funcC = funB("b");
print(funC("c"));

Object-Originted Programming

In Halang, you can use prototype to implement OOP, the type of prototype is usually a Dict, Dict is and internal object.

Let's assume a is an integer, Halang has included the prototype of Integer.

a + 1

The expression below is equal to:

a.__add__(1)

Because the prototype of integer includes __add__ method, so expression below can calculate the sum of a and 1. And you can get the prototype of integer and modify it, so that you can override the default __add__.

Future

In the future, many features will be add to HaLang. Such as

  1. break and continue statement
  2. Class support
  3. Yield
  4. binding to C lib
  5. ...

halang's People

Contributors

vincentdchan avatar

Watchers

James Cloos avatar ahnan 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.