Giter Club home page Giter Club logo

jcaml's Introduction

JCaml

Introduction

Welcome to the world of JCaml. Do you like to hump? Do you like to spit? JCaml has all these functions for you. To give you all the features of OCaml with the simplicity and ubiquity of JavaScript, JCaml has it all. This is our statically-typed, statically-scoped, superb language that cannot be beat. ##Features

This language has everything. It has pattern-matching, dictionary capabilities, list comprehension, recursive functions, higher-order functions, currying, and tuples. If you want it, it has it.

Syntax

JCaml {
    Program       =  Block
    Block         =  Stmt*
    Stmt          =  Decl | FuncDec | Exp | Print | FuncCall | Return
                  |  "if" Exp Block
                     ("else if" Exp Block)*
                     ("else" Block)?                             -- if

    Decl          =  "let" Type id "=" BinExp                    -- decl

    FuncDec       =  "let fun" id "=" Params "=>" (Type)? Body

    Type          =  "string" | "int" | "bool" | "char" | "float"

    FuncCall      =  id "(" Args ")"
    Arg           =  (id)?
    Args          =  ListOf<Arg, ",">

    Params        =  "(" (Param ("," Param)*)* ")"
    Param         =  id

    Body          =  ":" Block ";;"

    Exp           =  Exp relop MatchExp                         -- binary
                  |  MatchExp "?" MatchExp ":" MatchExp         -- ternary
                  |  MatchExp
    MatchExp      =  "match" id "with" Matches                  -- matchexp
                  |  BinExp
    BinExp        =  BinExp binop AddExp                        -- binary
                  |  AddExp
    AddExp        =  AddExp addop MullExp                       -- binary
                  |  MullExp
    MullExp       =  MullExp mullop PrefixExp                   -- binary
                  |  PrefixExp
    PrefixExp     =  prefixop ExpoExp                           -- binary
                  |  ExpoExp
    ExpoExp       =  ParenExp expop ExpoExp                     -- binary
                  |  ParenExp
    ParenExp      =  "(" AddExp ")"                             -- parens
                  |  numlit
                  |  Tuplit
                  |  List
                  |  stringlit
                  |  charlit

    Matches       =  ("|" Exp "->" Exp)+

    keyword       =  ("if" | "else" | "with" | "in" | "bool" | "int" | "string"
                  |  "double" | "float" | "long" | "list" | "hump" | "tuplit" | "spit") ~idrest

    prefixop      =  ~"--" "not" | "!" | "-"                    -- prefix

    id            =  ~keyword letter idrest*
    Tuplit        =  "(" BinExp "," BinExp ")"
    List          =  "[" BinExp ("," BinExp)* "]"               -- list
                  | "[]"
    Print         =  "spit" "(" BinExp ")"                      -- print
    Return        =  "hump" ParenExp
    idrest        =  "_" | alnum | "@" | "$"
    relop         =  ">" | ">=" | "==" | "!=" | "<" | "<="
    addop         =  "+" | "-" | "::"
    mullop        =  "*" | "/" | "%"
    expop         =  "^"
    binop         =  "||" | "or" | "&&" | "and"
    numlit        =  digit+
    char          =  escape
                  |  ~"\\" ~"\"" ~"\'" ~"\\n" any

    escape        = "\\\\" | "\\\"" | "\\'" | "\\n" | "\\t"
                  |  "\\u{" hexDigit+ "}"                       -- codepoint
    charlit       =  "'" (char | "\"") "'"
    stringlit     =  "\"" (char | "\'")* "\""

    space        := " " | "\t" | "\n" | "\r" | comment
    comment       =  "##" (~"\n" any)*
}

Examples

Hello World

JCaml JavaScript
spit("Hello, World");; console.log("Hello World");

Fibonacci Numbers

JCaml

let fun fib = (a) => int :
    if a == 0 or a == 1
        hump 1
    else
        hump fib(a-1) + fib(1-2)
;;

JavaScript

let fib = (a) => {
    if (a == 0 || a == 1) {
        return 1;
    } else {
        return fib(a - 1) + fib(a - 2);
    }
};

Fibonacci Numbers Memoized

JCaml

let fibDict = []
let fun fibMem = (a) => int :
    if a == 0 or a == 1
        hump 1
    else if a in fibDict
        hump fibDict[a]
    else
        fibDict[a] = fibMem(a-1) + fibMem(a-2)
        hump fibDict[a]
;;

JavaScript

let dictionary = {};
let fibMem = (a) => {
    if(a == 0 || a == 1) {
        return 1;
    } else if(a in dictionary) {
        return dictionary[a];
    } else {
        dictionary[a] = fibMem(a - 1) + fibMem(a - 2);
        return dictionary[a];
    }
};

Currying and Higher Order Functions

JCaml

let fun add = (a, b) => int :
    hump a + b;
;;
let fun add2 = add(2);;

Javascript

let add = (a) => {
    return (b) => {
        return a + b;
    };
};

let add2 = add(2);

Match Statement

JCaml

let fun count_occurences = (v, l) => int :
    match l with
    | [] -> 0
    | hd::[] ->  hump hd == v ? 1 : 0
    | hd::tl ->  hump hd == v ? 1 + count_occurences(v, tl) : count_occurences(v, tl)
;;

Javascript

let count_occurences = (v, l) => {
    let sum = 0;
    for (let value of l) {
      if(v === value) {
          sum++;
      }
    }
    return sum;
};

jcaml's People

Contributors

davidtparks avatar j-woodlee avatar mathewcsanders avatar rtoal avatar

Stargazers

 avatar  avatar  avatar

Watchers

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