Permalink
Browse files

Fuckyeah first commit. A bit of Ast, nothing too useful.

  • Loading branch information...
0 parents commit 30e7f32bd4c32bfd2679f6067d5d3f9219c7320e Amos Wenger committed Feb 6, 2011
Showing with 252 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +3 −0 samples/basic-exists/always_true.cr
  3. +13 −0 samples/basic-subtype/natural.cr
  4. +189 −0 source/Ast.ooc
  5. BIN source/AstTest
  6. +26 −0 source/AstTest.ooc
  7. +18 −0 source/Option.ooc
@@ -0,0 +1,3 @@
+.libs
+*_tmp
+*.[oac]
@@ -0,0 +1,3 @@
+
+rule ! \E (x | alwaysTrue(x) == false)
+
@@ -0,0 +1,13 @@
+
+Nat: cover from Int {
+ rule this >= 0
+}
+
+sqrt: func (a: Nat) {
+ sqrt2(a)
+}
+
+sqrt2: func (a: Nat) {
+
+}
+
@@ -0,0 +1,189 @@
+
+import structs/ArrayList
+
+import Option
+
+// Type constants
+voidType := Type new("Void")
+boolType := Type new("Bool")
+intType := Type new("Int")
+
+Type: class {
+ name: String
+ arguments := ArrayList<Type> new()
+
+ init: func (=name) {}
+}
+
+Stat: abstract class {
+
+}
+
+Expr: abstract class extends Stat {
+
+ /**
+ * We return an Option<Type> so we can avoid null types
+ * or at least enforce checking
+ */
+ getType: abstract func -> Option<Type>
+
+ toString: abstract func -> String
+}
+
+Name: class extends Expr {
+ name: String
+
+ init: func (=name) {}
+
+ getType: func -> Option<Type> {
+ return Option<Type> new()
+ }
+
+ toString: func -> String {
+ name
+ }
+}
+
+NumberLit: class extends Expr {
+ value: String
+
+ init: func(=value) {}
+
+ getType: func -> Option<Type> {
+ return Option<Type> new(intType)
+ }
+
+ toString: func -> String {
+ value
+ }
+}
+
+Comp: enum {
+ gt // greater than
+ gte // greater than or equal
+ lt // lesser than
+ lte // lesser than or equal
+ e // equal
+ ne // not equal
+}
+
+Comparison: class extends Expr {
+ comp: Comp
+ left, right: Expr
+
+ init: func (=comp, =left, =right) {}
+
+ getType: func -> Option<Type> {
+ return Option<Type> new(boolType)
+ }
+
+ toString: func -> String {
+ left toString() + " " + match comp {
+ case Comp gt => ">"
+ case Comp gte => ">="
+ case Comp lt => "<"
+ case Comp lte => "<="
+ case Comp e => "="
+ case Comp ne => "!="
+ } + " " + right toString()
+ // Yeeeeeeeeeeeehaa.
+ }
+}
+
+Rule: abstract class {
+
+ toString: abstract func -> String
+
+}
+
+ExprRule: class extends Rule {
+ expr: Expr
+
+ init: func(=expr) {}
+
+ toString: func -> String {
+ "rule %s" format(expr toString())
+ }
+}
+
+Var: class {
+ name: String
+
+ init: func (=name) {}
+}
+
+TypedVar: class extends Var {
+ type: Type
+
+ init: func (=name, =type) {}
+}
+
+Body: class {
+ rules := ArrayList<Rule> new()
+ stats := ArrayList<Stat> new()
+}
+
+FuncDecl: class {
+ arguments := ArrayList<Var> new()
+ retType := voidType
+ body := Body new()
+}
+
+TypeDecl: class {
+ name: String
+ arguments := ArrayList<String> new()
+
+ init: func(=name) {}
+
+ toString: func -> String {
+ "type %s" format(name)
+ // TODO: arguments (ie. generics)
+ }
+}
+
+CoverDecl: class extends TypeDecl {
+ under: String
+ underRef: CoverDecl // TODO: resolve? :D
+
+ rules := ArrayList<Rule> new()
+
+ init: func(=name, =under) {}
+
+ toString: func -> String {
+ b := Buffer new()
+ b append(super())
+ b append(" cover from %s {\n\n // rules:\n" format(under))
+ rules each(|rule| b append(" "). append(rule toString()). append("\n"))
+ b append("}\n\n")
+ b toString()
+ }
+}
+
+Module: class {
+ name: String
+
+ types := ArrayList<TypeDecl> new()
+ funcs := ArrayList<FuncDecl> new()
+ rules := ArrayList<Rule> new()
+
+ init: func(=name) {}
+
+ toString: func -> String {
+ b := Buffer new()
+ b append("// Module %s\n\n" format(name))
+ b append("// Types:\n")
+ types each(|type| b append(type toString()). append("\n"))
+
+ // TODO: FuncDecls and Rules
+ b toString()
+ }
+}
+
+
+
+
+
+
+
+
+
Binary file not shown.
@@ -0,0 +1,26 @@
+
+import Ast
+
+main: func {
+
+ m := Module new("natural")
+
+ // Nat: cover from Int {
+ natCover := CoverDecl new("Nat", "Int")
+ m types add(natCover)
+
+ // rule this >= 0
+ natCover rules add(ExprRule new(
+ Comparison new(
+ Comp gte,
+ Name new("this"),
+ NumberLit new("0")
+ )
+ ))
+
+ // }
+
+ m toString() println()
+
+}
+
@@ -0,0 +1,18 @@
+
+Option: class <T> {
+ data: T
+ none := false
+
+ init: func ~withData (=data) {}
+
+ init: func ~none {
+ none = true
+ }
+
+ unwrap: func (f: Func (T), g: Func ) {
+ if(none)
+ g()
+ else
+ f(data)
+ }
+}

0 comments on commit 30e7f32

Please sign in to comment.