Git Product home page Git Product logo

rascal's Introduction

Rascal Build Status

A (almost)functional interpreted language made by Rust

Features of functional languages

According with Haskel wiki the features of a functional language are:

  • Functions
  • Higher-order functions
  • Purity
  • Immutable data
  • Referential transparency
  • Lazy evaluation
  • Recursion

Use

rascal ./example.rl

Repl

rascal
>>

Install and run

git clone https://github.com/cristianoliveira/rascal.git
cd rascal
cargo build && cargo install
rascal ./example.rl

Motivation?

“If you don’t know how compilers work, then you don’t know how computers work. If you’re not 100% sure whether you know how compilers work, then you don’t know how they work.” — Steve Yegge

Structure

  • Integers: 0-9
  • Boolean: true, false
  • Imutables by default: let x = 1;
  • Mutables explicit: var x = 1;
  • Assign values: x = 0;
  • Blocks: { .. }
  • Operator: +, -, *, / and %
  • Comparison: ==,!=, >, <, and and or
  • If else: if 1==1 { .. else .. }
  • Loop: while 1==1 { .. }
  • Function: let foo = fn [x] { x + 1 }
  • Print: print (1+1)
  • Line Comments: # this is a comment

Example

First project euler challenge:

# Sum a number only whether it's multiple of 5 or 3

var sum = 0;
var number = 0;

let is_multiple = fn [x, y] { (x % y) == 0 };

while number < 10 {
  if is_multiple(number, 5) or is_multiple(number, 3) { sum = sum + number };
  number = number + 1
};

sum

Each statement requires a ; unless the last statement. Example of runnable code:

Integers expressions

let x = 20;
let y = 15;
let z = x + y;
z - 5

Result: 30

Bolean expressions

let x = 2;
let y = 1;
let z = x != y;
z == true

Result: true

If Else blocks

let x = 2;
var y = 0;

if x != 2 {
  y = 13
else
  y = 42
};

y == 42

Result: true

Loops

var y = 0;

while y < 4 {
  y = y + 1
};

y == 4

Result: true

Scope per block

var y = 0;

{
  var x = y + 1
};

x == 4

Error: "Variable x doesn't exist in this context"

Functions

let foo = fn [x] { x + 1 };

foo(10)

Result: 11

High Order Functions

let composed = fn [f] { f(10) };
let foo = fn [x] { x + 1 };

composed(foo)

Result: 11

Closures

let plus = fn[x, y] { x * y };
let teen = fn[f, b] { f(10, b) };

print(teen(plus, 5));
# prints 50

let plus_builder = fn[number] {
  let func = fn[y] { plus(number, y) }; func
};
# This quite ugly but the currently parser don't allow anonymous functions :/

let double  = plus_builder(2);
double(20)

Result: 40

Future implementations

  • Strings: support for strings
  • String comparison: support for compare strings
  • Return: return in the middle of a block
  • Stable REPL: run code without exiting for sintax errors

The Architecture

It is a simple interpreded language that walks on an AST executing the program. I would like to implement some bytecode, just for science sake, but for now this is fine. Example of an AST generated by code:

var a = 10;
var b = 1;

while b != 0 {
  if a > b {
    a = a - b;
  else
    b = b - a;
  }
};

return a

sintax

Licence

MIT

rascal's People

Contributors

cristianoliveira avatar

Stargazers

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

Watchers

 avatar  avatar

Forkers

hmleal

rascal's Issues

feature: if else blocks

begin
   mut a = 10;
   mut b = 1;

   while b != 0 begin
       if a > b then
         a = a - b;
       else
         b = b - a
       end
   end

   return a
end

sintax

Parabens pelo projeto!

Opa Cristiano, tudo bem?
Entao, primeiramente queria te dar os parabens por esse projeto. Por mais simples que ele seja, me ajudou a entender umas coisas sobre a assunto de compiladores/interpretadores e os algoritmos.

Eu tenho um plano de criar uma linguagem bem basica, para ajudar iniciantes a aprender programaçao. Tentei por si só criar algo e sem sucesso, até encontrar seu repositorio.

Ja fiz algumas modificações baseadas no seu codigo, como por exemplo:

  • palavras reservadas em pt-br
  • tudo é mutavel
    Proximos passos é ter uma cara mais de Python/Ruby.

Outra coisa bacana tambem, é que eu estou aprendendo Rust e que tambem estou formentando uma comunidade aqui no Brasil, em breve estaremos com hangout's voltado para estudos da linguagem.

https://github.com/rust-br/

refactor: revisit the function declarations

I am thinking that would be great if the language could express the function declarations in a more math way like:

sum = fn(x, y) { x + y };
return sum(1, 4)

or even:

fn sum(x, y) = { x + y };

return sum(1, 4) 

Just thinking

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.