Skip to content

Commit

Permalink
MiniGen initial import
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <[email protected]>
  • Loading branch information
Morriar committed Nov 9, 2011
0 parents commit a081de1
Show file tree
Hide file tree
Showing 86 changed files with 9,144 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/bin
.classpath
.project
.settings/
9 changes: 9 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MiniGen is a mini object oriented language dedicated to genericity experimentations.

Features:

* Simple and generic classes;
* Multiple formal types;
* Simple and multiple inheritance;
* Conservation of generic types at compile time (no erase);
* Comparison of generic types in linear time;
10 changes: 10 additions & 0 deletions examples/base_formal.minigen
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Fail : Formal type X already declared
#class B1[X, X] end

# Fail : Formal type conflict
#class Int end
#class Bool end
#class A[T] end
#class B[T] super A[T] end
#class C super A[Bool] end
#class D super B[Int], C end
48 changes: 48 additions & 0 deletions examples/base_inheritance.minigen
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Test simple inheritance
class A end
class B end
class C super A end
#class D super A end
#class F super C, D end
#class G super C, D end

# Test complex inheritance
#class Y1 end
#class Y2 super Y1 end
#class Y3 super Y2, Y4 end
#class Y4 super Y1 end
#class Y5 super Y4, Y2 end
#class Y6 super Y3, Y5 end

# Test generic super classes
class X1[X : Object, Y: A, Z:Object] end
class X2[X: Object] super X1[X, C, B] end

# Must fail: Bad bound
#class C1 end
#class C2 super C1 end
#class C3[T: C1] end
#class C4 super C3[Object] end


# Must fail: Herit himself
#class Z1 super Z1 end

# Must fail: Unknown super class
#class Z2 super Z999 end

# Must fail: Already declared as parent
#class Z3 super A, A end

# Must fail: Inheritance loop
#class Z4 super Z5 end
#class Z5 super Z4 end

# Must fail: Formal type arity
#class F1[X: Object] end
#class F2 super F1 end

# Must fail: Formal type not declared
#class F1[X: Object] end
#class F2 super F1[X] end

41 changes: 41 additions & 0 deletions examples/base_isa.minigen
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# For : Simple tests
class A1 end
class A2 super A1 end
class A3 super A2 end
class A4 super A3 end
class A5 end

# For medium tests
class Bool end
class Int end

class A[T, U] end
class B[T, U] super A[Object, Object] end
class C[T] super A[Bool, Int] end
class D super C[Object] end

# Simple tests
A1 isa A1 # prints TRUE
A2 isa A1 # prints TRUE
A4 isa A1 # prints TRUE
A5 isa A1 # prints FALSE
A1 isa A5 # prints FALSE

# Medium tests
Bool isa Int # prints FALSE
B[Int, Bool] isa A[Object, Object] # prints TRUE
B[Object, Object] isa A[Int, Bool] # prints FALSE
B[Object, Object] isa A[Object, Object] # prints TRUE
C[Int] isa A[Bool, Int] # prints TRUE
D isa A[Object, Object] # prints TRUE
C[Bool] isa A[Bool, Int] # prints TRUE

# Must fail : type undeclared
#Toto isa Bbb

# Must fail : type undeclared
#C[Toto] isa B[Int]

# Must fail : generic arity
#C isa A

32 changes: 32 additions & 0 deletions examples/hard_isa.minigen
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Number end
class Int super Number end
class Bool end

# For: Test 1
class X[T] end
class A[U] end
class B[V] super A[X[V]] end
class C[W] super B[X[W]] end
class D super C[Int] end

# For: Test 2
#class D[T] end
#class E[T] super D[T] end
#class F[T] super E[T], G[Bool] end
#class G[T] super D[T] end

# Test 1
Bool isa Int # prints FALSE
B[Int] isa A[Int] # prints FALSE
B[Int] isa A[X[Int]] # prints TRUE
B[Int] isa A[X[Object]] # prints TRUE
B[Int] isa A[X[Bool]] # prints FALSE
C[Int] isa A[X[X[Int]]] # prints TRUE
C[Int] isa A[X[Int]] # prints FALSE
A[X[X[Int]]] isa A[X[X[Int]]] #prints TRUE
A[X[X[Int]]] isa A[X[X[Bool]]] #prints FALSE
A[X[X[Int]]] isa A[X[X[Number]]] #prints TRUE
B[X[X[Int]]] isa A[X[X[Int]]] #prints FALSE

# Test 2
#F[Int] isa D[Bool] # prints TRUE
87 changes: 87 additions & 0 deletions examples/nitstdlib.minigen
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
class AbstractArray[E] super AbstractArrayRead[E], Sequence[E] end
class AbstractArrayRead[E] super SequenceRead[E] end
class AbstractSorter[E] super Object end
class AbstractString super AbstractArrayRead[Char] end
class ArrayCapable[E] super Object end
class Array[E] super AbstractArray[E], ArrayCapable[E] end
class ArrayIterator[E] super IndexedIterator[E] end
class ArrayMap[K, E] super CoupleMap[K, E] end
class ArraySet[E] super Set[E] end
class ArraySetIterator[E] super Iterator[E] end
class BMPattern super Pattern end
class Bool super Object end
class BufferedIStream super IStream end
class Buffer super AbstractString, Comparable, StringCapable, AbstractArray[Char] end
class Char super Discrete end
class Collection[E] super Object end
class ComparableSorter[E] super AbstractSorter[E] end
class Comparable super Object end
class Container[E] super Collection[E] end
class ContainerIterator[E] super Iterator[E] end
class Couple[F, S] super Object end
class CoupleMapIterator[K, E] super MapIterator[K, E] end
class CoupleMap[K, E] super Map[K, E] end
class Discrete super Comparable end
class FDIOStream super FDIStream, FDOStream, IOStream end
class FDIStream super FDStream, IStream end
class FDOStream super FDStream, OStream end
class FDStream super IOS end
class FileStat super Pointer end
class Float super Object end
class FStream super IOS, NativeFileCapable end
class HashCollection[K, N, E] super Collection[E], ArrayCapable[E] end
class HashMapIterator[K, V] super MapIterator[K, V] end
class HashMap[K, V] super Map[K, V], HashCollection[K, HashMapNode[K, V], V] end
class HashMapNode[K, V] super HashNode[K] end
class HashNode[K] super Object end
class HashSet[E] super Set[E], HashCollection[E, HashSetNode[E], E] end
class HashSetIterator[E] super Iterator[E] end
class HashSetNode[E] super HashNode[E] end
class IFStream super FStream, BufferedIStream end
class IndexedIterator[E] super MapIterator[Int, E] end
class Int super Discrete end
class IOProcess super IProcess, OProcess, IOStream end
class IOS super Object end
class IOStream super IStream, OStream end
class IProcess super Process, IStream end
class IStream super IOS end
class Iterator[E] super Object end
class IteratorRange[E] super Iterator[E] end
class List[E] super Sequence[E] end
class ListIterator[E] super IndexedIterator[E] end
class ListNode[E] super Container[E] end
class MapIterator[K, E] super Iterator[E] end
class Map[K, E] super RemovableCollection[E], MapRead[K, E] end
class MapRead[K, E] super Collection[E] end
class Match super Object end
class NaiveCollection[E] super Collection[E] end
class NativeArray[E] super Object end
class NativeFileCapable super Object end
class NativeFile super Pointer end
class NativeProcess super Pointer end
class NativeString super Object end
class OFStream super FStream, OStream end
class OProcess super Process, OStream end
class OStream super IOS end
class Pattern super Object end
class Pointer super Object end
class Process super Object end
class Range[E] super Collection[E] end
class RemovableCollection[E] super Collection[E] end
class Sequence[E] super SequenceRead[E], Map[Int, E], SimpleCollection[E] end
class SequenceRead[E] super MapRead[Int, E] end
class Set[E] super SimpleCollection[E] end
class SimpleCollection[E] super RemovableCollection[E] end
class Stderr super OFStream end
class Stdin super IFStream end
class Stdout super OFStream end
class StringCapable super Object end
class String super Pattern, Comparable, AbstractString end
class Symbol super Object end
class Sys super Object end

# Tests

Int isa Discrete # prints TRUE
AbstractString isa AbstractArrayRead[Char] # prints TRUE
AbstractString isa MapRead[Int, Char] # prints TRUE
14 changes: 14 additions & 0 deletions examples/test.minigen
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Number end
class Bool end
class Int super Number end

class A[T, U] end

class B[V] super A[Object, V] end

class C[X, Y, Z] super B[Z] end



A[Bool, Bool] isa A[Object, Object]
A[Object, Object] isa A[Bool, Bool]
89 changes: 89 additions & 0 deletions grammar/minigen.sablecc3
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
Package minigen.syntax3;

Helpers

upper = ['A'..'Z'];
lower = ['a'..'z'];
letter = [upper+lower];
digit = ['0'..'9'];

cr = 13;
lf = 10;
tab = 9;

eol = cr | lf | cr lf;

blank = ' ' | eol | tab;

not_eol = [[0..127]-[cr+lf]];

Tokens

comma = ',';

lb = '[';
rb = ']';

kend = 'end';
kisa = 'isa';
kclass = 'class';
ksuper = 'super';
knew = 'new';

name = upper (letter | digit)*;

blanks = blank+;
comment = '#' not_eol*;

Ignored Tokens
blanks, comment;

Productions

program =
[classes]: class_decl* [instrs]:instr*;

class_decl =
kclass name formal_decls? super_decls? kend;

formal_decls =
lb formal_decl+ rb;

formal_decl =
name additional_formal_types*;

additional_formal_types =
comma name;

type =
name generic_part?;

generic_part =
lb generic_types rb;

generic_types =
type additional_types*;

additional_types =
comma type;

super_decls =
ksuper super_type additional_supers*;

additional_supers =
comma super_type;

super_type =
name super_generic_part?;

super_generic_part =
lb super_generic_types rb;

super_generic_types =
super_type super_additional_types*;

super_additional_types =
comma super_type;

instr =
{isa} [left]:type kisa [right]:type;
Binary file added lib/sablecc.jar
Binary file not shown.
45 changes: 45 additions & 0 deletions src/minigen/ClassAnalysis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package minigen;

import minigen.model.Class;
import minigen.model.ObjectClass;
import minigen.model.Model;
import minigen.syntax3.analysis.DepthFirstAdapter;
import minigen.syntax3.node.AClassDecl;

public class ClassAnalysis extends DepthFirstAdapter {

private Model model;
private int currentId;

public ClassAnalysis(Model classScope) {
this.currentId = 0;
this.model = classScope;

//Reserve id 0 for Object class
reserveCurrentId();

// Declare Object class
this.model.declareClass(null, ObjectClass.getInstance());
}

@Override
public void caseAClassDecl(AClassDecl node) {

//Get the class id
int id = reserveCurrentId();

//Declare class
model.declareClass(node, new Class(node.getName().getText().trim(),
node.getKclass(), id));
}

/*
* Returns and increments the current id
*/
private int reserveCurrentId() {
int id = this.currentId;
this.currentId++;
return id;
}

}
Loading

0 comments on commit a081de1

Please sign in to comment.