Giter Site home page Giter Site logo

fayrant-lang's Introduction

🎩 Fayrant-lang

Simple, interpreted, dynamically-typed programming language

Authors:

Basic syntax

~ this is comment

~ variable declaration
var something;
var something2 = 123;

~ simple types
something = null;

something = true;
something = false;

something = 1234;           ~ all numbers are 64-bit floating point (double)
something = 3.14159;
something = 0b100001011001; ~ binary literal
something = 0xDeadC0de;     ~ hexadecimal literal

something = "simple string";
something = "string with \n \t \\ \" \{ \} escapes";
something = "string with unicode escapes \u{65} \u{0x41}";
something = "string with interpolation { 2 + 3 + something2 }";
something = "string { "with { "nested" } interpolation" }";

~ unary and binary operators
something = !something;
something = -something;
something = @1234;    ~ conversion to string
something = #"1234";  ~ conversion to number

~ arithmetic
something = 1 + 2 - 3;
something = 1 * 2 + 3 / 4;
something = 4 \ 3;      ~ same as 3 / 4;
something = 2 ^ 3 ^ 4;  ~ exponentiation
something = 5 % 3;

~ comparison
something = 2 < 3;
something = 2 > 3;
something = 2 <= 3;
something = 2 >= 3;

something = 2 == 3;
something = 2 != 3;
something = "asd" == "def";
something = "asd" == 3;
something = "asd" == null;

something = "Hello " ++ "world!";  ~ string concatenation

~ logical
something = true & false;
something = true | false;

~ assignment operators
something = 1;
something += 2;
something -= 3;
something *= 4;
something /= 5;
something \= 6;
something %= 7;
something ^= 8;
something &= true;
something |= false;
something ++= "asdf";

~ basic io
something = input();  ~ returns string
print(something);     ~ prints anything

~ control flow

if (something > 5) {
  print("hello");
}

if (something == 7) {
  print("seven");
} else {
  print("not seven");
}

while (something > 0) {
  something -= 1;
}

for (var i = 0; i < 3; i += 1) {
  print(i);
}

while (true) {
  if (true) {
    break;
  }
}

for (var i = 0; i < 3; i += 1) {
  if (i == 2) {
    continue;
  }
  print(i);
}

Functions example

func factorial_iter(x) {
  if (x < 1) {
    return 1;
  }
  var res = 1;
  while (x > 1) {
    res *= x;
    x -= 1;
  }
  return res;
}

func factorial_rec(x) {
  if (x < 1) {
    return 1;
  }
  return x * factorial_rec(x - 1);
}

var x = #input();
if (x == null | x < 1) {
  print("Invalid number");
  return;
}
print("Iter: {factorial_iter(x)}");
print("Rec: {factorial_rec(x)}");

Class example

class Vec2d {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  func plus(other) {
    return Vec2d(this.x + other.x, this.y + other.y);
  }
  
  func norm() {
    return (this.x ^ 2 + this.y ^ 2) ^ 0.5;
  }
  
  func str() {
    return "Vec[{this.x},{this.y}]";
  } 
}

var v1 = Vec2d(3, 4);
var v2 = Vec2d(4, 5);
var v3 = v1.plus(v2);

print("{v1.str()} + {v2.str()} = {v2.str()}")

FAQ:

Why Fayrant name?

It's a tribute to this outstanding beverage

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.