Giter Club home page Giter Club logo

valentine's Introduction

Valentine

An interpreted toy language using ANTLR4 for parser/lexer/AST generation

Currently supports:

  • Dynamic typing

  • Arithmetic, relational and boolean operations

  • Print statement

  • Lists, integers, boolean values

  • First class functions (with closures) : Functions can be assigned to variables, be returned by other functions and be used as arguments.The value of the last expression is returned as the value of the function (like in Lisps)

  • Anonymous Functions: Anonymous function calls and definitions can be used as function arguments and/or be bound to variables

  • Lazy evaluation using delay thunks: Potentially infinite lists can be created this way (though this limited by the maximum recursion depth / stack size of the JVM)

Check the *.vlnt files for sample programs

Quick tutorial of the syntax/features:

  • Variable assignment:
  # Identifiers may be formed from any combination of lower case characters (a-z) 
  # except for the language's keywords: if,def,lambda,true,false,list etc
  a = 5 #assigned to literal/atom
  b = f(a) #assigned to an evaluated value
  c = lambda(x,y,z){ x + y +z } #binding an anonymous function
  d = (lambda (x) { x + x }(100)) # calling an anonymous function "on-the-spot" 
  • Function definition:
# see test.vlnt for more elaborate examples of functions

def f (x){ #typical function definition
  x + x
}  

def fibo(x){ # an example function : a fibonacci series generator
  # last evaluated element is returned as the value of a function
  if x == 1 or x == 0{
    x
  }
  else{
    fibo(x - 1) + fibo(x - 2)
  }
}

lambda (x,y){ x / y } #anonymous function declaration
lambda (func,x){ func(x) } (f,100) # anonymous function call and a high order function example
  • Lists
a = 10
b = list(5,a,lambda(x){ 3 * x }(5)) 
#Useful list operations: 
#Result argument is the list to append the results to (can be an empty list: list() )
map(function,list)
length(list)
filter(predicate,list)
contains(list,key)

#FOLDS:
#The function argument must be a binary function
#initValue is usually the identity value of the function: 0 for add/sub, 1 for mul/div , true for and , false for or
reduceR(function,list,initialValue) 
reduceL(function,list,initialValue)

A handy visualisation of right and left folds respectively:

Image of right fold

Image of left fold

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.