Skip to content

Instantly share code, notes, and snippets.

@pathikrit
Last active December 19, 2015 13:40
Show Gist options
  • Select an option

  • Save pathikrit/5963942 to your computer and use it in GitHub Desktop.

Select an option

Save pathikrit/5963942 to your computer and use it in GitHub Desktop.
$lang: a meta^2 programming language

$lang (pronounced dollar-lang) is a beautiful minimalistic readable programming language inspired by Good (or Crazy) Ideas™ of other languages.

Complete list of keywords in $lang:

  1. logical values: true, false
  • primitive data types: text, number, logical
  • control-flow statements: if, then, else, while
  • when
  • exists
  • is
  • `s
  • logical operators: not, and, or, xor

$lang code is written in a Markdown document with the extension .dl.md (inspired by Literate CoffeeScript(Literate CoffeeScript)). The compiler treats any indented blocks (Markdown's way of indicating source code) as code, and ignores the rest as comments. In fact, you can execute this document itself.

Brief tour of the language:

x: 5 # You can have additional inline comments

y: 7                    # declare a variable y

print x !               # prints 7 - the ! character denotes call the function print with parameter x
print(x)                # functions can also be called with C-style parenthesis instead of !

y: x + y                # update y

print x !               # prints 12

print x <= 13 !         # prints yes

print x > 15 !          # prints no

print 0 < x < 1000 !    # prints yes

print x = 15 !          # prints yes 

Note in x = 15 here we are checking if x is equal to 15 and not assigning. Assignments are done through the : operator e.g. x: 15 assigns 15 to x and x = 15 checks if x is 15 or not.

z: 'Hi!'        # a text - can be either surrounded by single or double quotes

z: "This is
a multiline
text
"	            # a multiline text

z: 'We can "mix" \'quotes\' too'

Functions definitions in $lang are very light weight and are declared almost the same way as variables:

hello: (name) -> print "Hello {{name}}" !     # hello is a function that takes in a name

hello "World" !                               # prints Hello World

printAndAdd: (x,y) ->                         # a multiline function
	z: x + y                    
	print z !
	z                                         # the last line is the "return" value of the function

sayMyName: (name: "Rihanna") -> print "My name is {{name}}" !

sayMyName!                    # prints My name is Rihanna
sayMyName 'Rick' !            # prints My name is Rick

howdy: (a,b) ->	print "{{a}} {{b}}" !

howdy "ping", "pong" !          # prints ping pong

All text can be actually treated as mustache templates

All data in $lang are either primitives like:

  1. text e.g. "hello"
  • number e.g. 1 or -2.23
  • logical e.g. true and false

Or are complex data structures (lists and maps and objects) that can be constructed from the primitive data types

Lists

x: (23.90, 'hi')             # x is a list 
y: (1, 2, "hello", x)        # y is a list - a list can contain other lists

print y~1 !        # prints 1 (yes the first element is indexed at 1 and not 0)
print y~2 !        # prints 2
print y~3 !        # prints hello
print y~4 !        # prints (23.90, hi)
print y~5 !        # prints ? (we only have 4 elements)
print y~-1 !       # prints the last element (23.90, hi)

Data can be nested:

rick:
	name: "Rick"
	age: 29
	say: (what) -> print("{name} says {what}"
	pets
	

print rick~age !
rick~say "this is awesome" !

Nulls:

a: ?
print a exists !

$lang is optionally typed (inspired by Dart) which means you can initially write untyped code but later you can add type checking as preconditions (inspired by Cobra).

Types in $lang are treated as special cases of preconditions/annotations that help static analysis e.g.

fib: (n) ->
    where:
        n is number     # static type inferance makes sure everyone who calls fib must call with a number
        n > 0           # is not guaranteed to be always caught during compile time 
        
    if n <= 1 then n else fib(n-1) + fib(n-2)    

fib 5 ! would compile but fib "hello" ! won't compile.

fib -7 ! won't compile but fib(if yes then -7 else 7) would compile but would crash during run-time.

$lang has 3 fundamental types:

  • logical - Represents truth values yes and no
  • number - Represents arbitrary precision numbers e.g. 0, -1 or 3.1415926943493492
  • text - Represents things like hello world or a

and 2 composite types

  • lists e.g. (23.90, 'hi', yes)
  • maps:

One of the most versatile features of $lang (inspired by Katahdin) is that you can embed any other languages inside $lang:

myName: 'Rick'

someJavaCode:                                         # whatever is printed to standard out is the result
    <java file="Test.java" args="{{myName}}">
        public class Test {
            public static void main(String[] args) {
                System.out.println("Java says " + args[0])
            }
        }
    </java>
    
print someJavaCode !         # prints Java says Rick

$lang can support any other programming langiage through compiler plugins.

Inspired by Katahdin

$lang unlike most other languages has no memory limits. When it runs out of memory, it silently uses up disk space. When it runs out of disk space, it can smartly start using network or cluster or even remote space e.g. on S3. All this is done through various compiler level plugins e.g. this code won't run out of memory

You can ofcourse run it in the traditional mode of limited RAM by giving certain runtime arguments.

Sometimes we need to persist or store data between runs of a program. This is usually handled through databases and ORMs. $lang on the other hand does not make any distinguishment between data that is persistent and data that is not.

  1. static analysis / advanced types e.g. factorial(7) would compile but factorial(-7) won't compile in my language

  2. never ran out of memory - memory is something abstracted away - if it runs out of ram, it uses disk, if out of disk, it uses cluster etc etc - all this pluggable behind the scenes

  3. has built in database engine - kind of related to above - you don't have to think about sql or mongo or redis - these are all pluggable at language level e.g. you can declare

  4. typing is optional - when you add types to something - only code paths that uses that something need to be typed else everything can be untyped

  5. Inspired by F# - support for units e.g you can do things like (5.km + 7.inches)/(8.seconds) -

  6. Track differences between pure functions and procedures (side effects)

  7. No exposed traditional compiler or run time - just one environment which let's you run you $lang code. The $lang environment is modular and supports

  8. Reactive/bound variables

  9. https://news.ycombinator.com/item?id=6398274

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment