Giter Site home page Giter Site logo

dysphasia's Introduction

Dysphasia

Build Status Code Climate Test Coverage

At a certain point in every programmer's life, one develops the desire to create a programming language. I am no different. This is extremely experimental, and best thought of as an art project.

Dysphasia is a simple language that compiles via LLVM.

Download

git clone https://github.com/sminnee/dysphasia.git 
cd dysphasia
npm install

On OSX, you can use Homebrew to install LLVM, which is a dependency. Note that you have to use the llvm36 package, not the llvm one.

brew install llvm36

If you install this package, then you will get llvm-config-3.6, clang++-3.6, llc-3.6 and opt-3.6 in your path. If these tools have different binary names, please set the LLVM_CONFIG, CLANG, LLC and OPT environment variables.

Try it out

It doesn't do very much right, now, but this will show a parse tree:

node main.js examples/simple.dp

This will compile to examples/simple, which you can then run:

examples/simple

Language features

Because this is in development, many basic features are missing, but this is what is implemented so far.

Function blocks

Functions are defined by writing a function name, followed by a set of statements in braces. Function names must start with a letter, and subsequent characters should be alphanumerics or _.

main {
  return 1;
}

Expressions

Basic mathematical expressions are supported, with +, -, *, /, and brackets for precedence:

12 * (2 + 4)

String expressions

Strings are double quoted:

"Hello!"

Strings can be concatenated using +. Any other type that is concatenated to a string will be casted to a string:

"I've called this" + 5 + " times!" 5 + " o'clock" "Lucky number" + 5

If/then/else

If/then/else synax is similar to C:

main {
  if(1) {
    puts("Hello world");
  } else {
    puts("Goodbye world");
  }
}

Loops

For loops will iterate on an array. You can simply provide a list, as a way of repeating a statement a number of times:

for(1..10) { puts("Show me ten times"); }

Use

The "use" statement can import a C function, such as puts. Right now it only works with functions that take a single char* argument.

use puts(string);
use print(string, ...);
main {
  puts("hello world");
  printf("I can count to %i!\n", 5);
  return 0;
}

dysphasia's People

Contributors

bnolan avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

bnolan

dysphasia's Issues

array literals and operations

Two features:

  • Array literals that don't simply get inlined as constants
  • Array operations: multiplication with a similarly sized array, or with a scalar

The following script:

x = [1, 2, 3, 5];
y = x * 2;
z = y * y;

for(i in z) {
  puts(i);
}

Should output:

4
16
36
100

array.reduce

Let's add a companion to array.map and we can bask in our functional goodness.

main {
  square = (x) => x*x;
  sum = (x) => x+x;
  sumSquares = a => a.map(square).reduce(sum);
  puts(sumSquares([1,2,3])); // 14
}

Ideally, the output of map can be piped into reduce without first creating an intermediary array, so that the compiled code is more of this form:

sumSquares(a) {
  for(i in a) {
    mapped = i*i;
    reduced = reduced+mapped;
  }
  return reduced;
}

array.map

Amend the range synax to use [ and ]. It will make chaining easier. Implement array.map, using a lambda as a function argument.

main {
   for(i in [1..5].map(x | x*x)) puts(i);
}

Better type inferencing

In the following example:

main {
   for(x in [1..5]) {
    puts(square(x));
   }
}

square(x) => x*x;

The following type inferences should be made:

  • x is int because it's used to iterate on an int array
  • square takes and int argument because it's called with an int argument
  • square returns an int because it's x*x return operation is an int

Conflicts should throw a syntax error.

Single-assignment variables

Although mutable variables are something I'd like to stay away from initially, single-assignment variables should be supported, to do stuff like this:

main {
   x = [1,4,9,5];
   for(i in x) {
     printf("x[%i] = %i", i, x[i]);
   }
}

Once compiled, the printf should probably still refer directly to a const array.

Inferred generic function

In this example:

main {
    puts(square(2));
    puts(square(3.5));
   }
}

square(x) => x*x;

The following type inferencing should occur:

  • square(x) should first be inferred to be int(int)
  • It should secondly be inferred to be float(float)
  • A separate copy of the function should be generated for each argument signature

Internally the the code should get transformed to something like this:

main {
    puts(int square<int>(2));
    puts(float square<float>(3.5));
   }
}

T square<T>(T x) => x*x;
// expanded
int square<int>(int x) => x*x;
float square<float>(float x) => x*x;

Note: It might be easier if all method calls have type suffixes added to them, regardless of whether there are multi-type calls.

Lambdas

Add support for lambdas, as a first step towards adding array.map. Could use arrow or guard syntax. While we're add it, allow single-statement for loops, for the example below :-)

main {
  int(int) square = (x) => x * x;
  int(int) cube = (x | x * x * x);
  for(i in 1..5) puts(square(i));
  for(i in 1..5) puts(cube(i));
}

Don't worry about closure variable binding for now.

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.