Skip to content

Instantly share code, notes, and snippets.

@fenhl
Created August 24, 2011 22:14
Show Gist options
  • Save fenhl/1169424 to your computer and use it in GitHub Desktop.
Save fenhl/1169424 to your computer and use it in GitHub Desktop.
Γαμμα
# collatz.ga
this will print out a tree representing Collatz's conjecture in reverse
This code shows that one can write Gamma programs in different styles
or even, as in this case, in the style of another programming language
Python version: http://codepad.org/ASIFuwku #
uint LIMIT ← 100.
obj Collatz ← «
(Collatz) initWithValue:uint val ← “
self ← [Collatz new].
[self value] ← val.
if (val < LIMIT) {
[self trunk] ← [Collatz initWithValue:val * 2],
if ((val > 4) & (val % 6 = 4)) {
[self branch] ← [Collatz initWithValue:(val - 1) / 3]
}
}.
return self
”.
(void) printInnerWithPrefix:string prefix ← “
print prefix · [self value].
if ([self branch]) {
string newPrefix ← " " … [[self value] length] … " " · "| ".
[[self branch] printInnerWithPrefix:newPrefix]
}.
[[self trunk] printInnerWithPrefix:prefix]
”.
(void) prints ← “
[self printInnerWithPrefix:"| "]
».
[[Collatz initWithValue:1] prints]
(* This is the grammar draft (not up to date) of the Γαμμα (Gamma) programming language by Fenhl. *)
(* The grammar is described in EBNF. Comments and whitespace are ignored. *)
program = [ expr , { terminator , expr } ] ;
expr = group | var | call | keyword ;
group = "{" , program , "}" ;
var = literal | ( [ type ] , varname | call , [ "←" , ( var | call ) ] ) ;
literal = array | math | string | timeint ;
array = "<" , { var } , ">" ;
math = ( "(" , math , ")" ) | ( [ "-" ] , num | var , [ op , math ] ) ;
num = digit , { digit } , [ "." , digit , { digit } , [ "_" , digit , { digit } ] ] ;
op = "+" | "-" | "*" | "×" | "÷" | "/" | "%" | "^" | "√" ;
string = "␊" | ( '"' , { char } , '"' ) | var , { string } ;
timeint = num , s | min | h | d | a ;
type = ( alpha , { alpha } ) | ( "<" , alpha , { alpha } , ">" ) ;
varname = alpha , { alpha | digit } ;
call = "[" , var , alpha , { alpha } , [ ":" , var , { alpha , { alpha } , ":" , var } ] , "]" ;
keyword = "break" | "comma" | "continue" | ( "for (" , var , "~" , var , "~" , var , ")" , group )
| ( "fork" , group ) | ( "idle" , [ timeint ] )
| ( "if (" , var , ")" , group , [ "else" , group ] )
| ( "on (" , messagesig | var , ")" , group ) | ( "repeat" , group ) | ( "return" , [ var ] )
| "skip" | ( "until (" , messagesig | var , ")" , group ) | ( "when (" , var , ")" , group )
| ( "while (" , var , ")" , group ) ;
messagesig = "+" | "-" , "[" , var , methsig , "]" ;
methsig = alpha , { alpha } , [ ":" , { alpha , { alpha } , ":" } ] ;
terminator = "." | "," ;
# This simple Gamma program outputs "Hello, world!" on the command line when called from the command line. #
return "Hello, world!"
# This simple Gamma program outputs "Hello, world!" on the command line if it has access. #
print "Hello, world!"
# This example shows how Gamma handles primitive data types. #
int number ← 1.
int otherNumber ← number.
# otherNumber is now 1. #
number ← 2.
# otherNumber is now 2. #
otherNumber ← |number|.
number ← 3
# OtherNumber is still 2. #
# As you can see, primitive data types in Gamma work like pointers in other languages. #
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment