langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,2 @@
def foo = 42; // immutable by default
mutable bar = "O'Malleys"; // mutable because you asked it to be

View file

@ -0,0 +1,15 @@
type im_string
val create : int -> im_string
val make : int -> char -> im_string
val of_string : string -> im_string
val to_string : im_string -> string
val copy : im_string -> im_string
val sub : im_string -> int -> int -> im_string
val length : im_string -> int
val get : im_string -> int -> char
val iter : (char -> unit) -> im_string -> unit
val escaped : im_string -> im_string
val index : im_string -> char -> int
val contains : im_string -> char -> bool
val print : im_string -> unit

View file

@ -0,0 +1,16 @@
type im_string = string
let create = String.create
let make = String.make
let copy = String.copy
let sub = String.sub
let length = String.length
let get = String.get
let iter = String.iter
let escaped = String.escaped
let index = String.index
let contains = String.contains
let of_string s = s
let to_string s = s
let print = print_string

View file

@ -0,0 +1,4 @@
constant $pi = 3.14159;
constant $msg = "Hello World";
constant @arr = (1, 2, 3, 4, 5);

View file

@ -0,0 +1 @@
constant fibonacci = 0, 1, *+* ... *;

View file

@ -0,0 +1,2 @@
my $pi is readonly = 3 + rand;
my $pi ::= 3 + rand; # same thing, SSA-ish form

View file

@ -0,0 +1,14 @@
sub sum (Num $x, Num $y) {
$x += $y; # ERROR
}
# Explicitly ask for pass-by-reference semantics
sub addto (Num $x is rw, Num $y) {
$x += $y; # ok, propagated back to caller
}
# Explicitly ask for pass-by-value semantics
sub sum (Num $x is copy, Num $y) {
$x += $y; # ok, but NOT propagated back to caller
$x;
}

View file

@ -0,0 +1,2 @@
$me = "myname"
%age = 35

View file

@ -0,0 +1,3 @@
#i_Const1 = 11
#i_Const2 = 3.1415
#i_Const3 = "A'm a string"

View file

@ -0,0 +1,78 @@
;Enforced immutability Variable-Class
Interface PBVariable ; Interface for any value of this type
Get() ; Get the current value
Set(Value.i) ; Set (if allowed) a new value in this variable
ToString.s() ; Transferee the value to a string.
Destroy() ; Destructor
EndInterface
Structure PBV_Structure ; The *VTable structure
Get.i
Set.i
ToString.i
Destroy.i
EndStructure
Structure PBVar
*VirtualTable.PBV_Structure
Value.i
EndStructure
;- Functions for any PBVariable
Procedure immutable_get(*Self.PBVar)
ProcedureReturn *Self\Value
EndProcedure
Procedure immutable_set(*Self.PBVar, N.i)
ProcedureReturn #False
EndProcedure
Procedure.s immutable_ToString(*Self.PBVar)
ProcedureReturn Str(*Self\Value)
EndProcedure
Procedure DestroyImmutabe(*Self.PBVar)
FreeMemory(*Self)
EndProcedure
;- Init an OO-Table
DataSection
VTable:
Data.i @immutable_get()
Data.i @immutable_set()
Data.i @immutable_ToString()
Data.i @DestroyImmutabe()
EndDataSection
;- Create-Class
Procedure CreateImmutabe(Init.i=0)
Define *p.PBVar
*p=AllocateMemory(SizeOf(PBVar))
*p\VirtualTable = ?VTable
*p\Value = Init
ProcedureReturn *p
EndProcedure
;- **************
;- Test the Code
;- Initiate two Immutabe variables
*v1.PBVariable = CreateImmutabe()
*v2.PBVariable = CreateImmutabe(24)
;- Present therir content
Debug *v1\ToString() ; = 0
Debug *v2\ToString() ; = 24
;- Try to change the variables
*v1\Set(314) ; Try to change the value, which is not permitted
*v2\Set(7)
; Present the values again
Debug Str(*v1\Get()) ; = 0
Debug Str(*v2\Get()) ; = 24
;- And clean up
*v1\Destroy()
*v2\Destroy()

View file

@ -0,0 +1,3 @@
const integer: foo is 42;
const string: bar is "bar";
const blahtype: blah is blahvalue;

View file

@ -0,0 +1,2 @@
PIE=APPLE
readonly PIE

View file

@ -0,0 +1,2 @@
define Pi=3.14;
Pi:= 3.15; \causes a compile error: statement starting with a constant