Code Monkey home page Code Monkey logo

interpreter-egg's Introduction

Egg

Build Status

A little language named Egg implemented above javascript.

It's simple. But support function closures, high-order functions and so on. And it could include nearly any logic you could imagine.

Just for fun, origin from my favorite Eloquent Javascript

All just for fun.

Syntax

All is expressions. Valid expression are string/number/word/application.

do(define (total, 0), 
   define (count, 1),
   while(<(count, 11),
         do(define(total, +(total, count)),
            define(count, +(count, 1)))),
   print(total))

Comment

# used for comment

+(1, 2) # plus 1 and 2

do

Run multiple expressions one by one. Return the last expression.

do(expr1[, expr2[, expr3[, ...]]])

values

Valid values include string, number, true and false.

1
"hack"
true
false

word

A name represent variables like values, function, nearly everything.

a
hehe
define
do
print

if

Return different value depending on conditions;

if(condition_expression, true_expression, false_expression)

while

Just like while in other languages. Return false.

while(condition_expression, expression)

define

Define a variable in current scope. Return expression's returned value

define(word, expression)

set

Set variables out of current scope, if there isn't exist, throw ReferenceError. Return the newly set value.

set(word, expression)

+ - * / == < >

Just as you see in other languages. These calculations are supported.

+(1, 2)
>(4, -(2, 1))

fun

define a function. the last arguments will be the function body, all other arguments will be seen as arguments for this function.

do(define(add, fun(a, b, +(a, b))),
   print(add(1, 2)))

REPL

Run egg.js,just input your code line by line. Ctrl-D or Ctrl-Cto exit:

┌─[reverland@reverland-R478-R429] - [~/tmp/eloquentjs] - [2015-11-12 03:16:54]
└─[0] <git:(master 976b113✱✈) > node egg.js
> do(define (total, 0), 
> ..define (count, 1),
> ..while(<(count, 11),
> ....do(define(total, +(total, count)),
> ......define(count, +(count, 1)))),
> ..print(total))
55

TODO

  • more tests...
  • repl
  • compiler
  • better parser. learn how Lex and Yacc works.
  • evaluate when parse(JIT? 😂)
  • reread [Eloquent Javascript] and have fun with it 😊

interpreter-egg's People

Contributors

reverland 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.