Przeglądaj źródła

import files

master
Ian Piumarta 1 rok temu
rodzic
commit
ec29069aa4
7 zmienionych plików z 3099 dodań i 0 usunięć
  1. +59
    -0
      Makefile
  2. +34
    -0
      bench-sieve.txt
  3. +10
    -0
      bench.txt
  4. +2509
    -0
      minproto.leg
  5. +156
    -0
      test.ref
  6. +329
    -0
      test.txt
  7. +2
    -0
      test2.txt

+ 59
- 0
Makefile Wyświetl plik

@ -0,0 +1,59 @@
OFLAGS += -Wall -Wno-unused -O3 -DNDEBUG
GFLAGS += -Wall -Wno-unused -g
PFLAGS += $(OFLAGS) -pg
CFLAGS += -I/opt/local/include
LDLIBS += -L/opt/local/lib
LDLIBS += -lgc -lm
MAIN = minproto
all : $(MAIN)
% : %.c
$(CC) $(GFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS)
%-opt : %.c
$(CC) $(OFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS)
%-prof : %.c
$(CC) $(PFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS)
%.c : %.leg
leg -o $@ $<
test : $(MAIN)
./$(MAIN) < test.txt | tee test.out
@diff test.ref test.out && echo '\012--- PASSED ---'
testref : $(MAIN)
./$(MAIN) < test.txt | tee test.ref
bench : $(MAIN)-opt
time ./$(MAIN)-opt -O bench.txt
time ./$(MAIN)-opt -O bench.txt
time ./$(MAIN)-opt -O bench.txt
sieve : $(MAIN)-opt
time ./$(MAIN)-opt -O bench-sieve.txt
time ./$(MAIN)-opt -O bench-sieve.txt
time ./$(MAIN)-opt -O bench-sieve.txt
profile : $(MAIN)-prof
./$(MAIN)-prof -O < bench.txt
gprof ./$(MAIN)-prof gmon.out
FILES = Makefile $(MAIN).leg bench.txt test.txt test2.txt
checkpoint : .FORCE
tar cvfz ckpt/minproto-$(shell date "+%Y%m%d-%H%M%S").tgz $(FILES)
clean : .FORCE
rm -rf $(MAIN) $(MAIN)-opt $(MAIN)-prof *.o *.dSYM *.sync-conflict-*
spotless : clean
rm -f *~
stats : .FORCE
@echo $(shell tr -d ' \t\\' < minproto.leg | sort -u | wc -l) lines of code
.FORCE :

+ 34
- 0
bench-sieve.txt Wyświetl plik

@ -0,0 +1,34 @@
numbers = 8192; // 1028 primes
repeats = 50;
count = 0;
then = cputime();
evals = evaluations();
for (n = 0; n < repeats; n = n + 1) {
print("\r", n, "/", repeats);
sieve = [];
count = 0;
for (i in numbers) sieve.push(nil);
for (i = 2; i < numbers; i = i + 1) {
if (!sieve[i]) {
// print(i, " ");
count = count + 1;
for (k = i+i; k < numbers; k = k+i)
sieve[k] = #t;
}
}
count;
}
evals = evaluations() - evals;
now = cputime();
secs = now - then;
print("\r", count, " primes in the first ", numbers, " numbers\n");
print(evals, " nodes evaluated in ",
secs, " seconds = ",
round(evals / secs), " nodes/sec\n");

+ 10
- 0
bench.txt Wyświetl plik

@ -0,0 +1,10 @@
nfib = (n) { if (n < 2) 1; else nfib(n-1) + nfib(n-2) + 1; };
then = cputime();
calls = nfib(32);
now = cputime();
secs = now - then;
print(calls, " calls in ",
secs, " seconds = ",
round(calls / secs), " calls/sec\n");

+ 2509
- 0
minproto.leg
Plik diff jest za duży
Wyświetl plik


+ 156
- 0
test.ref Wyświetl plik

@ -0,0 +1,156 @@
nil 42 3.140000 string Symbol newline
Object => <Object>
Lambda => <Lambda>
Closure => <Closure>
o = Object.new() => <<Object>>
o = Object.new(foo:42, bar: 666) => <<Object>>
| bar: 666
| foo: 42
o.foo => 42
Point.new() => <<Point>>
Point.new(x: 3, y: 4) => <<Point>>
| y: 4
| x: 3
twice 21 is 42
double => <closure>
Point.new(x:3, y:4).magnitude() => 5.000000
<<?>>
| self: nil =>
| self: nil
| n: 39
| 0: 40
40
<<?>>
| self: nil =>
| self: nil
| n: 40
| 0: 40
41
<<?>>
| self: nil =>
| self: nil
| n: 41
| 0: 40
42
<<?>>
| self: nil =>
| self: nil
| n: 42
| 0: 40
43
nil no
1 yes
hello yes
15
1973
42 42 42 42
ok
ok
42
----
MyType.__eval__() invoked
42
<closure>
====
42
hello
nil
<Undefined>
<Object>
nil
[KEY: __delegate__
KEY: y
KEY: x
]
Symbol
<Symbol>
<Object>
nil
POINT <Point>
P <<Object>>
P <<Point>>
Q <<Point>>
P.mag 13.000000
<<Object>>
| bar: 666
| foo: 42
| 0: 1
| 1: <<Object>>
| | 0: 2
| | 1: 3
| | 2: 42
[KEY: __delegate__
KEY: bar
KEY: foo
]
5
7
0
1
2
0
1
0: a
2
0: a
1: b
3
0: a
1: b
2: c
12
0: 104
1: 101
2: 108
3: 108
4: 111
5: 44
6: 32
7: 119
8: 111
9: 114
10: 108
11: 100
hello, world
hallo, world
hally, world
hally, wirld
READ = f() { print(__env__()) }
f();
0 30 t
<<Stream>>
f 102 ( 40 ) 41 32 { 123 32 p 112 r 114 i 105 n 110 t 116 ( 40 _ 95 _ 95 e 101 n 110 v 118 _ 95 _ 95 ( 40 ) 41 ) 41 32 } 125
10 f 102 ( 40 ) 41 ; 59
10
<<Binop>>
| operation: 15
| 0: 6
| 1: 7
42
42
6 * 7
MACRO table <<Object>>
| test: <closure>
define testfun
MACRO EVAL test with 1 and 2
MACRO EVAL test with three and four
call testfun
REPLACEMENT 1
REPLACEMENT three
0 1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9
65 66 67 68 69
1 two 3 four
11: n * factorial(n - 1)
10: if (n < 2) "1" else n * factorial(n - 1)
9: factorial(n - 1)
8: n * factorial(n - 1)
7: if (n < 2) "1" else n * factorial(n - 1)
6: factorial(n - 1)
5: n * factorial(n - 1)
4: if (n < 2) "1" else n * factorial(n - 1)
3: factorial(n - 1)
2: n * factorial(n - 1)
1: if (n < 2) "1" else n * factorial(n - 1)
0: factorial(5)

+ 329
- 0
test.txt Wyświetl plik

@ -0,0 +1,329 @@
// nil is the undefined object
// integers, floats, strings are as usual
// #<identifier> is a symbol literal
print(nil, " ", 42, " ", 3.14, " string ", #Symbol, " newline \n");
// the prototypical object types used in the implementation have global names
// e.g., Object is the prototypical object at the root of the delegation hierarchy
print("Object => ", Object, "\n"); // this prints as <Object>
print("Lambda => ", Lambda, "\n"); // this prints as <Lambda>
print("Closure => ", Closure, "\n"); // this prints as <Closure>
// create a new object instance by asking an existing one to be the delegate for a new instance
// e.g., to create a new object that delegates to the prototype Object...
o = Object.new();
print("o = Object.new() => ", o, "\n");
// this prints as <<Object>> because the number of <> surrounding the name tells you
// how many levels of delegation were needed before the __name__ property was found
// arguments can be positional and/or key:value pairs
// for the new() method, the arguments become the properties of the newly created object
o = Object.new(foo: 42, bar: 666);
print("o = Object.new(foo:42, bar: 666) => ", o, "\n", full: 1);
print("o.foo => ", o.foo, "\n");
// to create a new type make a prototype with a __name__ that delegates to another prototype
Point = Object.new(__name__: #Point);
// objects can be created from the new prototype because it delegates to the Object prototype,
// in other words the new prototype 'inherits' Object.new()
// objects created from the new prototype will inherit its __name__ property
print("Point.new() => ", Point.new(), "\n");
print("Point.new(x: 3, y: 4) => ", Point.new(x: 3, y: 4), "\n", full: 1);
// anonymous functions are written: "(parameters...) { statements... }"
// all statements return values
// the last statement in a block provides a return value for the entire function
double = (x) { x+x };
print("twice 21 is ", double(21), "\n");
// use the keyword argument "full:x" (where x is non-nil) to make the print primitive
// recursively print the entire contents of any objects it encounters
print("double => ", double, "\n", full:1);
// anonymous functions installed as properties of a prototype become methods
// for all objects that delegate to the prototype
Point.magnitude() { sqrt(self.x * self.x + self.y * self.y) }
m = Point.new(x: 3, y: 4).magnitude();
print("Point.new(x:3, y:4).magnitude() => ", m, "\n");
// functions close over their dynamic environment when created
// (yes, I know, something needs to be done about all the nasty semicolons)
makeCounter(n)
{
n = n - 1; // make the counter return n the first time it is called
() { print(__env__(), "\n", full:1); n = n + 1 } // the counter is an anonymous function, closing over n, that increments n
}
counter = makeCounter(40);
print(counter(), "\n");
print(counter(), "\n");
print(counter(), "\n");
print(counter(), "\n");
test(x) { print(x, " "); if (x) print("yes\n") else print("no\n") }
test(nil);
test(1);
test("hello");
nfib(n) { if (n < 2) 1 else nfib(n-1) + nfib(n-2) + 1 }
print(nfib(5), "\n");
print(nfib(15), "\n");
assert(x) {
if (!(eval(x))) {
print("\nassertion failed: ", codeString(x), "\n");
exit(1);
}
}
makeForm(assert);
refute(x) {
if (eval(x)) {
print("\nrefutation failed: ", codeString(x), "\n");
exit(1);
}
}
makeForm(refute);
assert(1 == 1);
refute(1 == 0);
assert(0 < 1); refute(1 < 1); refute(2 < 1);
assert(0 <= 1); assert(1 <= 1); refute(2 <= 1);
refute(0 >= 1); assert(1 >= 1); assert(2 >= 1);
refute(0 > 1); refute(1 > 1); assert(2 > 1);
refute(0 == 1); assert(1 == 1); refute(2 == 1);
assert(0 != 1); refute(1 != 1); assert(2 != 1);
assert(16 << 0 == 16); assert(16 << 1 == 32); assert(16 << 2 == 64); assert(16 << 3 == 128);
assert(16 >> 0 == 16); assert(16 >> 1 == 8); assert(16 >> 2 == 4); assert(16 >> 3 == 2);
print(0b101010, " ");
print(052, " ");
print(42, " ");
print(0x2a, "\n");
bin__(n, b, w) {
if (n >= 2 || w > 1) bin__(truncate(n / 2), b, w - 1);
print(n % 2);
}
assert((0b1110 | 0b0111) == 0b1111);
assert((0b1110 ^ 0b0111) == 0b1001);
assert((0b1110 & 0b0111) == 0b0110);
nt = 0;
nf = 0;
t() { nt = nt + 1; #t }
f() { nf = nf + 1; nil }
refute(f() || f()); assert(nt == 0); assert(nf == 2);
assert(f() || t()); assert(nt == 1); assert(nf == 3);
assert(t() || f()); assert(nt == 2); assert(nf == 3);
assert(t() || t()); assert(nt == 3); assert(nf == 3);
refute(f() && f()); assert(nt == 3); assert(nf == 4);
refute(f() && t()); assert(nt == 3); assert(nf == 5);
refute(t() && f()); assert(nt == 4); assert(nf == 6);
assert(t() && t()); assert(nt == 6); assert(nf == 6);
1 || ({ print("fail\n"); exit(1) });
nil && ({ print("fail\n"); exit(1) });
nil || print("ok\n");
1 && print("ok\n");
print(eval(42), "\n");
MyType = Object.new(__name__: #MyType);
print("----\n");
MyType.__eval__(self) {
print("MyType.__eval__() invoked\n");
42;
}
myType = MyType.new();
print(eval(myType), "\n");
Object.print() { print(self) }
Object.println() { self.print(); print("\n") }
(){}.println();
x = 42;
print("====\n");
(42).println();
"hello".println();
nil.println();
nil.__delegate__.println();
nil.__delegate__.__delegate__.println();
nil.__delegate__.__delegate__.__delegate__.println();
Object.dump()
{
print("[");
keys = self.keys();
for (key in keys) print("KEY: ", key, "\n");
print("]\n");
}
p = Point.new(x: 3, y: 4);
p.dump();
#Symbol.println();
#Symbol.__delegate__.println();
#Symbol.__delegate__.__delegate__.println();
#Symbol.__delegate__.__delegate__.__delegate__.println();
print("POINT ", Point, "\n");
p = [x:5, y:12];
print("P ", p, "\n");
p[#__delegate__] = Point;
print("P ", p, "\n");
q = [__delegate__:Point, x:5, y:12];
print("Q ", q, "\n");
print("P.mag ", p.magnitude(), "\n");
o = [bar:666, 1, [2, 3, 6*7], foo:42];
print(o, "\n", full:1);
o.dump();
print(len("Hello"), "\n");
print(len(#Goodbye), "\n");
print(len([]), "\n");
print(len([1]), "\n");
print(len([1, 2]), "\n");
printelts(x)
{
n = len(x);
print(n, "\n");
i = 0;
while (i < n) {
print(i, ": ", x[i], "\n");
i = i + 1;
}
x;
}
f() {
printelts(__env__());
}
f();
f("a");
f("a", "b");
f("a", "b", "c");
printelts("hello, world");
s = "hello, world\n"; print(s);
s[1] = 97; print(s);
s[4] = ord("y"); print(s);
s[8] = ord("i"); print(s);
print("READ = ", readfile("test2.txt"));
Object.subtype(name) { self.new(__name__: name) }
Stream = Object.subtype(#Stream);
newStream(string) {
self = Stream.new(
content: string,
position: 0,
limit: len(string)
);
print(self.position, " ", self.limit, " ", !self.atEnd(), "\n");
self;
}
Stream.atEnd() { self.position >= self.limit }
Stream.peek() { !self.atEnd() && self.content[self.position] }
Stream.next() {
!self.atEnd() && {
c = self.content[self.position];
self.position = self.position + 1;
c;
}
}
s = newStream(readfile("test2.txt"));
print(s, "\n");
while (!s.atEnd()) print(chr(s.peek()), " ", s.next(), " ");
print("\n");
x = (`6*7;);
print(x, "\n", full:1);
print(x.__eval__(), "\n", full:1);
print(eval(x), "\n");
print(codeString(x), "\n");
Symbol.macros.test = (x, y)
{
print("MACRO EVAL test with ", x, " and ", y, "\n");
`{print("REPLACEMENT ", @x, "\n"); @y }
};
print("MACRO table ", Symbol.macros, "\n", full:1);
print("define testfun\n");
testfun() {
test(1, 2);
test("three", "four");
}
print("call testfun\n");
testfun();
for (i from 0 to 10) print(i, " "); print("\n");
for (i from 10 to 0) print(i, " "); print("\n");
for (i in 10) print(i, " "); print("\n");
for (i in "ABCDE") print(i, " "); print("\n");
for (i in [1, "two", 3, "four"]) print(i, " "); print("\n");
factorial(n) { if (n < 2) "1" else n * factorial(n-1) }
factorial(5);

+ 2
- 0
test2.txt Wyświetl plik

@ -0,0 +1,2 @@
f() { print(__env__()) }
f();

Ładowanie…
Anuluj
Zapisz