Designing my own Programming Language
Posted: Tue Aug 11, 2009 9:23 pm
So I've just about decided how my programming language will work. It's inspired by smalltalk, ocaml, and haskell though I promise it's quite unique. So here's an explanation of the planned syntax.
1. Everything is a block. Blocks may be defined in this way:
2. New blocks may be obtained by copying existing blocks. A colon represents copying a block.
3. Blocks have two parts: a data section and a code section. The data section is always formatted like dataName: DataType, dataName: DataType. but can have any variable number of data fields.
4. When blocks are called, the value of their data is set, and their code is ran. The previous code is called thusly:
5. Nested blocks! (And thus OOP). Unlike C functions you can access nested blocks from outside by specifying their parent. Nested blocks have direct access to their parent's data and other blocks (no "this" or "self" needed).
6. Blocks may be copied and then immediately redefined. (Inheritance)
7. Type system giving the coder however much strictness they want.
8. Pattern matching when calling blocks! Uses blocks without names to "split" the data section into different patterns to match. Sorta like the "match x with" of ocaml. Blocks are defined not only by their name but also by which given data parameters fit which block the best.
Example programs:
I think one thing it is missing is a bit more syntactic sugar, but it's as good as smalltalk on that front anyways. Do I hear a "yeah" or a "nay" to changing to a python-style indentation? I like it, but I know a lot of people don't, so braces are okay.
1. Everything is a block. Blocks may be defined in this way:
Code: Select all
BlockName
{
...blockdefinition...
}
Code: Select all
n: 5. // n is a copy of "5"
myList: List. // myList is a copy of "List"
Code: Select all
Multiply
{
number1: Int, number2: Int. // data section
^number1 * number2. // code section ('^' means 'yield')
}
Code: Select all
>> Multiply 4, 5.
20
Code: Select all
Stack
{
size: Int,
items: List.
Push
{
item.
items append item.
}
Pop
{
. // a single period indicates no data section for the block
^items removeAt (items length).
}
}
>> Stack 10. // sets the stack's "size", doesn't alter "items"
>> Stack Push 7.
>> Stack Push 22.
>> Stack Push 5.
>> Stack Pop
5
>> Stack items
[7; 22]
Code: Select all
Cat: Animal
{
...redefine some old stuff...
...add some new stuff...
}
Code: Select all
>> x: Int.
>> x = 5.
>> x = 6.
>> x = 'a'.
ERROR: 'x' is type 'Int', not type ''a''
>> y: Object. // note: this is the same as just saying 'y.' but more verbose.
>> y = 7.
>> y = "A string!".
>> z: 42.
>> z = 21.
ERROR: 'z' is type '42', not type '21'
>> z = 42.
>> z = "fourty-two".
ERROR: 'z' is type '42', not type '"fourty-two"'
Code: Select all
Factorial
{
{ n: 0. ^1. }
{ n: Int. ^n * Factorial (n - 1). }
}
>> Factorial 5
120
>> Factorial "abc"
ERROR: 'n' is type '0' or type 'Int', not type '"abc"'
Code: Select all
// prints "Hello, world!" as many times as specified from the command line
HelloWorld
{ . PrintLine "Hello, world!". }
Main
{
sysArgs: List.
1 to (List at 1 toInt), HelloWorld.
}
Code: Select all
// gives us the GCD of two numbers a and b
GreatestCommonDivisor
{
{a: 0, b: Int. ^b. }
{a: Int, b: 0. ^a. }
{
a: Int, b: Int.
(a > b) ifTrue
{ ^GreatestCommonDivisor (a - b) b. }
else
{ ^GreatestCommonDivisor (b - a) a. }.
}
}