├── README.md ├── README.txt └── src └── jat ├── lang ├── Value.java ├── AType.java ├── AtomicClass.java ├── ArrayInit.java ├── TransactionRestart.java ├── FutureAlreadySetException.java ├── Tasks.java ├── AtomicFuture.java ├── AtomicState.java ├── UnitTest.java ├── Util.java ├── ValueArray.java ├── Transaction.java └── Atom.java ├── test ├── SSN.jat ├── SSN.java ├── Person.jat ├── Person.java ├── Atomicizer.jat ├── Atomicizer.java ├── Test.jat └── Test.java ├── cfail ├── Fail1.jat ├── Static.jat ├── Fail2.jat ├── NoExtends.jat └── NoExtends2.jat ├── comp ├── Field.java ├── NullWriter.java ├── Location.java ├── CompFailException.java ├── IndentWriter.java ├── Jat.peg ├── AtomJat.java ├── Type.java └── Jat.java └── ant ├── Unit.java └── Jat.java /README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | Type "ant" to build. 2 | -------------------------------------------------------------------------------- /src/jat/lang/Value.java: -------------------------------------------------------------------------------- 1 | package jat.lang; 2 | 3 | public interface Value {} 4 | -------------------------------------------------------------------------------- /src/jat/lang/AType.java: -------------------------------------------------------------------------------- 1 | package jat.lang; 2 | 3 | enum AType { READ, WRITE, UPDATE } 4 | -------------------------------------------------------------------------------- /src/jat/lang/AtomicClass.java: -------------------------------------------------------------------------------- 1 | package jat.lang; 2 | 3 | public interface AtomicClass {} 4 | -------------------------------------------------------------------------------- /src/jat/test/SSN.jat: -------------------------------------------------------------------------------- 1 | package jat.test; 2 | 3 | public value SSN { 4 | int ssn = 1112223333; 5 | } 6 | -------------------------------------------------------------------------------- /src/jat/cfail/Fail1.jat: -------------------------------------------------------------------------------- 1 | @CompFail("not allowed in Value",3) 2 | public value Fail1 { 3 | Object o; 4 | } 5 | -------------------------------------------------------------------------------- /src/jat/lang/ArrayInit.java: -------------------------------------------------------------------------------- 1 | package jat.lang; 2 | 3 | public interface ArrayInit { 4 | T call(int n); 5 | } 6 | -------------------------------------------------------------------------------- /src/jat/test/SSN.java: -------------------------------------------------------------------------------- 1 | package jat.test; 2 | public class SSN implements jat.lang.Value { 3 | int ssn =1112223333 ; 4 | } 5 | -------------------------------------------------------------------------------- /src/jat/lang/TransactionRestart.java: -------------------------------------------------------------------------------- 1 | package jat.lang; 2 | 3 | public class TransactionRestart extends RuntimeException { 4 | } 5 | -------------------------------------------------------------------------------- /src/jat/lang/FutureAlreadySetException.java: -------------------------------------------------------------------------------- 1 | package jat.lang; 2 | 3 | public class FutureAlreadySetException extends RuntimeException {} 4 | -------------------------------------------------------------------------------- /src/jat/lang/Tasks.java: -------------------------------------------------------------------------------- 1 | package jat.lang; 2 | 3 | public class Tasks { 4 | public static void schedule(Runnable r) { 5 | new Thread(r).start(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/jat/cfail/Static.jat: -------------------------------------------------------------------------------- 1 | package jat.cfail; 2 | 3 | @CompFail("Static fields must be Value or AtomicClass at",5) 4 | public class Static { 5 | static int[] n= null; 6 | } 7 | -------------------------------------------------------------------------------- /src/jat/test/Person.jat: -------------------------------------------------------------------------------- 1 | package jat.test; 2 | 3 | public value Person { 4 | int a = 3; 5 | int b = 10; 6 | String name=""; 7 | SSN sn = new SSN(); 8 | public void run() {} 9 | } 10 | -------------------------------------------------------------------------------- /src/jat/cfail/Fail2.jat: -------------------------------------------------------------------------------- 1 | import jat.lang.Atom; 2 | 3 | @CompFail("jat.lang.Atom cannot contain 'java.lang.Object'",6) 4 | public class Fail2 { 5 | void foo() { 6 | Atom ao = new Atom(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/jat/comp/Field.java: -------------------------------------------------------------------------------- 1 | package jat.comp; 2 | 3 | public class Field { 4 | public Type type; 5 | boolean isStatic; 6 | boolean isPublic; 7 | boolean isPrivate; 8 | boolean isProtected; 9 | boolean isFinal; 10 | } 11 | -------------------------------------------------------------------------------- /src/jat/test/Person.java: -------------------------------------------------------------------------------- 1 | package jat.test; 2 | public class Person implements jat.lang.Value { 3 | int a =3 ; 4 | int b =10 ; 5 | java.lang.String name ="" ; 6 | jat.test.SSN sn =new jat.test.SSN(); 7 | public void run (){ 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/jat/cfail/NoExtends.jat: -------------------------------------------------------------------------------- 1 | package jat.cfail; 2 | 3 | import jat.test.Person; 4 | 5 | // The extending class should be a value 6 | @CompFail("Cannot extend a value with a class",7) 7 | public class NoExtends extends Person { 8 | // doesn't matter what's here 9 | } 10 | -------------------------------------------------------------------------------- /src/jat/cfail/NoExtends2.jat: -------------------------------------------------------------------------------- 1 | package jat.cfail; 2 | 3 | import jat.test.Atomicizer; 4 | 5 | // The extending class should be a value 6 | @CompFail("Cannot extend an atomic class with a class",7) 7 | public class NoExtends2 extends Atomicizer { 8 | // doesn't matter what's here 9 | } 10 | -------------------------------------------------------------------------------- /src/jat/comp/NullWriter.java: -------------------------------------------------------------------------------- 1 | package jat.comp; 2 | 3 | public class NullWriter extends java.io.Writer { 4 | @Override 5 | public void write(char[] a,int b,int c) { 6 | } 7 | @Override 8 | public void flush() {} 9 | @Override 10 | public void close() {} 11 | } 12 | -------------------------------------------------------------------------------- /src/jat/test/Atomicizer.jat: -------------------------------------------------------------------------------- 1 | package jat.test; 2 | 3 | /** 4 | * Types of variables may be: 5 | * primitive types, values, and 6 | * atomic classes. 7 | */ 8 | public atomic class Atomicizer { 9 | atomic int a; 10 | atomic int b; 11 | //atomic Object o; 12 | final int c = 3; 13 | atomic Person p; 14 | } 15 | -------------------------------------------------------------------------------- /src/jat/comp/Location.java: -------------------------------------------------------------------------------- 1 | package jat.comp; 2 | 3 | public class Location { 4 | final String srcFile; 5 | final int line; 6 | public Location(String srcFile,int line) { 7 | this.srcFile = srcFile; 8 | this.line = line; 9 | } 10 | public String toString() { 11 | return srcFile + ":" + (line <= 0 ? "?" : line); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/jat/test/Atomicizer.java: -------------------------------------------------------------------------------- 1 | package jat.test; 2 | public class Atomicizer implements jat.lang.AtomicClass { 3 | final jat.lang.Atom a = new jat.lang.Atom(); 4 | final jat.lang.Atom b = new jat.lang.Atom(); 5 | final int c =3 ; 6 | final jat.lang.Atom p = new jat.lang.Atom(); 7 | } 8 | -------------------------------------------------------------------------------- /src/jat/comp/CompFailException.java: -------------------------------------------------------------------------------- 1 | package jat.comp; 2 | 3 | import edu.lsu.cct.piraha.Group; 4 | 5 | public class CompFailException extends RuntimeException { 6 | public int line; 7 | CompFailException(String msg,Jat j,Group g) { 8 | this(msg,j.srcFile,g.getLine()); 9 | this.line = g.getLine(); 10 | } 11 | CompFailException(String msg) { 12 | super(msg); 13 | } 14 | CompFailException(String msg,String file,int line) { 15 | super(msg+" at "+file+":"+line); 16 | this.line = line; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/jat/lang/AtomicFuture.java: -------------------------------------------------------------------------------- 1 | package jat.lang; 2 | 3 | /** 4 | * AtomicFutures can only be set inside 5 | * transactions, and can be 6 | * read outside them. They preserve 7 | * the value last seen inside a 8 | * transaction. 9 | */ 10 | public class AtomicFuture { 11 | private T data; 12 | private boolean isset = false; 13 | public void set(final T v,final Transaction t) { 14 | if(isset) throw new FutureAlreadySetException(); 15 | data = v; 16 | isset = true; 17 | } 18 | public T get(Transaction t) { 19 | return data; 20 | } 21 | public T get() { 22 | return data; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/jat/lang/AtomicState.java: -------------------------------------------------------------------------------- 1 | package jat.lang; 2 | 3 | public class AtomicState implements Comparable> { 4 | AType atype; 5 | T data; 6 | T origData; 7 | Atom atom; 8 | public AtomicState(T data,AType atype,Atom atom) { 9 | this.data = this.origData = data; 10 | this.atype = atype; 11 | this.atom = atom; 12 | } 13 | public int compareTo(AtomicState that) { 14 | int a = System.identityHashCode(this); 15 | int b = System.identityHashCode(that); 16 | if(a < b) return 1; 17 | else if(a > b) return -1; 18 | else return 0; 19 | } 20 | public void set() { 21 | atom.data = data; 22 | atom.state.set(null); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/jat/lang/UnitTest.java: -------------------------------------------------------------------------------- 1 | package jat.lang; 2 | 3 | import jat.comp.Jat; 4 | 5 | public abstract class UnitTest { 6 | public class UnitTestFail extends RuntimeException { 7 | public UnitTestFail(String msg) { super(msg); } 8 | } 9 | public abstract void runTest(); 10 | public void assertTrue(boolean b,String msg) { 11 | if(!b) throw new UnitTestFail("AssertTrue: "+msg); 12 | } 13 | public void assertFalse(boolean b,String msg) { 14 | if(b) throw new UnitTestFail("AssertFalse: "+msg); 15 | } 16 | public static void main(String[] args) throws Exception { 17 | Jat j = new Jat(); 18 | j.compile(args[0],false); 19 | String className = j.pkg + "." + j.objName; 20 | Class c = Class.forName(className); 21 | try { 22 | UnitTest ut = (UnitTest)c.newInstance(); 23 | ut.runTest(); 24 | } catch(ClassCastException cce) { 25 | return; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/jat/comp/IndentWriter.java: -------------------------------------------------------------------------------- 1 | package jat.comp; 2 | 3 | import java.io.Writer; 4 | import java.io.IOException; 5 | 6 | public class IndentWriter extends Writer { 7 | private int indent; 8 | private boolean flag = false; 9 | private Writer w; 10 | public IndentWriter(Writer w) { 11 | this.w = w; 12 | } 13 | @Override 14 | public void write(char[] buf,int off,int len) throws IOException { 15 | int n = off + len; 16 | for(int i=off;i implements Value { 8 | public final int length; 9 | Object[] data; 10 | public ValueArray(T[] init) { 11 | length = init.length; 12 | data = new Object[init.length]; 13 | for(int i=0;i list) { 17 | length = list.size(); 18 | data = new Object[list.size()]; 19 | for(int i=0;i iter) { 23 | List list = new ArrayList(); 24 | while(iter.hasNext()) 25 | list.add(iter.next()); 26 | length = list.size(); 27 | data = new Object[list.size()]; 28 | for(int i=0;i ai) { 32 | length = n; 33 | data = new Object[n]; 34 | for(int i=0;i> states = new TreeSet>(); 13 | 14 | public void add(AtomicState t) { 15 | states.add(t); 16 | } 17 | public void finish() { 18 | boolean succeed = true; 19 | List> list = new ArrayList>(); 20 | for(AtomicState astate : states) { 21 | astate.atom.lock.lock(); 22 | list.add(astate); 23 | if(astate.atype == AType.READ && !Util.equals(astate.origData,astate.atom.data)) { 24 | succeed = false; 25 | break; 26 | } 27 | } 28 | if(succeed) { 29 | for(AtomicState astate : states) { 30 | astate.set(); 31 | } 32 | } 33 | for(AtomicState astate : states) { 34 | astate.atom.lock.unlock(); 35 | } 36 | if(!succeed) { 37 | throw new TransactionRestart(); 38 | } 39 | for(Runnable r : tasks) { 40 | r.run(); 41 | } 42 | tasks.clear(); 43 | states.clear(); 44 | } 45 | 46 | List tasks = new ArrayList(); 47 | public void addTask(Runnable r) { 48 | tasks.add(r); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/jat/lang/Atom.java: -------------------------------------------------------------------------------- 1 | package jat.lang; 2 | 3 | import java.util.concurrent.locks.ReentrantLock; 4 | 5 | public class Atom implements Comparable { 6 | T data; 7 | 8 | final ThreadLocal> state = new ThreadLocal>(); 9 | final ReentrantLock lock = new ReentrantLock(); 10 | 11 | public Atom() {} 12 | 13 | public Atom(T t) { 14 | data = t; 15 | } 16 | 17 | /** 18 | * Get a variable within a transaction. 19 | */ 20 | public T get(Transaction tr) { 21 | AtomicState astate = state.get(); 22 | if(astate == null) { 23 | astate = new AtomicState(data,AType.READ,this); 24 | state.set(astate); 25 | tr.add(astate); 26 | } 27 | return astate.data; 28 | } 29 | 30 | /** 31 | * Set a variable within a transaction. 32 | **/ 33 | public void set(T t,Transaction tr) { 34 | AtomicState astate = state.get(); 35 | if(astate == null) { 36 | astate = new AtomicState(data,AType.WRITE,this); 37 | state.set(astate); 38 | tr.add(astate); 39 | } 40 | astate.data = t; 41 | } 42 | 43 | public int compareTo(T t) { 44 | int a = System.identityHashCode(this); 45 | int b = System.identityHashCode(t); 46 | if(a > b) return 1; 47 | else if(a < b) return -1; 48 | else return 0; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/jat/test/Test.jat: -------------------------------------------------------------------------------- 1 | package jat.test; 2 | 3 | from java.io 4 | import IOException, File; 5 | import jat.lang.AtomicFuture; 6 | 7 | public class Test extends jat.lang.UnitTest { 8 | ValueArray va = 9 | new ValueArray(new Integer[]{1,2,3}); 10 | static int vv = 3; 11 | atomic int ai; 12 | atomic int aj=4; 13 | Value v = new Person(); 14 | int k; 15 | int k2 = 4+2*(3-1); 16 | Test foo() { 17 | return this; 18 | } 19 | public Test() { 20 | atomic { 21 | ai = 3; 22 | } 23 | } 24 | public void runTest() { 25 | System.out.println("Hello, world"); 26 | for(int i=0;i<10;i+=1) { 27 | System.out.println("i="+i); 28 | } 29 | final int nn = 3+1; 30 | int n = nn; 31 | int i; 32 | for(i=0;i "+nn); 41 | } 42 | while(n > 0) { 43 | n -= 1; 44 | } 45 | //async(System.out.println("async")); 46 | assertTrue(n == 0,"n == null"); 47 | Test t = new Test(); 48 | Test t2 = new Test().foo().foo(); 49 | Integer n1 = 3; 50 | Double n2 = 3.0; 51 | assertTrue(n1 == n2,"n1 == n2"); 52 | 53 | String s1 = "foo"; 54 | String s2 = s1+""; 55 | assertTrue(s1 == s2,"s1 == s2"); 56 | Test test = new Test(); 57 | final AtomicFuture ss = new AtomicFuture(); 58 | atomic { 59 | test.ai += 2; 60 | ss = test.ai.get(); 61 | AtomicFuture s = sync{ return "fish"; }; 62 | sync{ System.out.println("sstran="+ss.get()); }; 63 | sync{ System.out.println("stran="+s.get()); }; 64 | } 65 | System.out.println("ss="+ss.get()); 66 | assertTrue(ss.get() == 5,"ss.get()==5"); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/jat/ant/Unit.java: -------------------------------------------------------------------------------- 1 | package jat.ant; 2 | 3 | import org.apache.tools.ant.Project; 4 | import org.apache.tools.ant.Task; 5 | import org.apache.tools.ant.BuildException; 6 | import org.apache.tools.ant.types.FileSet; 7 | import org.apache.tools.ant.types.Path; 8 | import org.apache.tools.ant.types.CommandlineJava; 9 | import org.apache.tools.ant.types.Resource; 10 | import org.apache.tools.ant.types.Assertions; 11 | import org.apache.tools.ant.taskdefs.ExecuteJava; 12 | import org.apache.tools.ant.taskdefs.Redirector; 13 | import org.apache.tools.ant.taskdefs.Javac; 14 | import org.apache.tools.ant.taskdefs.Java; 15 | 16 | import java.io.File; 17 | import java.io.FileWriter; 18 | import java.io.IOException; 19 | 20 | import java.util.Iterator; 21 | import java.util.Map; 22 | import java.util.HashMap; 23 | 24 | import java.lang.reflect.Method; 25 | 26 | import jat.lang.UnitTest; 27 | 28 | public class Unit extends Task { 29 | /** 30 | * Attempt to run the target program. If the 31 | * program is not a UnitTest, the test is considered 32 | * successful. If it is a UnitTest, the runTest() 33 | * method is called. If the method completes without 34 | * throwing an exception, it is counted as successful. 35 | */ 36 | public void run(String jatFile) { 37 | try { 38 | Redirector redirector = new Redirector(this); 39 | CommandlineJava command = new CommandlineJava(); 40 | command.createVmArgument().setLine("-ea"); 41 | command.createArgument().setLine(jatFile); 42 | command.setClassname("jat.lang.UnitTest"); 43 | ExecuteJava exe = new ExecuteJava(); 44 | exe.setClasspath(cp); 45 | exe.setJavaCommand(command.getJavaCommand()); 46 | exe.setSystemProperties(command.getSystemProperties()); 47 | redirector.createStreams(); 48 | exe.execute(getProject()); 49 | redirector.complete(); 50 | if (exe.killedProcess()) { 51 | throw new BuildException("Timeout"); 52 | } 53 | } catch(Exception e) { 54 | throw new BuildException(e); 55 | } 56 | } 57 | FileSet fs; 58 | public void addFileset(FileSet fs) { 59 | this.fs = fs; 60 | } 61 | Path cp; 62 | public void addClasspath(Path cp) { 63 | this.cp = cp; 64 | } 65 | public void init() { 66 | } 67 | @SuppressWarnings("unchecked") 68 | public void execute() { 69 | // There's no way to make the warning go away 70 | Iterator iter = fs.iterator(); 71 | while(iter.hasNext()) { 72 | Resource fr = iter.next(); 73 | run(fr.toString()); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/jat/comp/Jat.peg: -------------------------------------------------------------------------------- 1 | skipper = \b([ \t\r\n]|//.*|/\*(\*[^/]|[^*])*\*/)* 2 | 3 | fname = {base}{suffix} 4 | base = [^\\.]* 5 | suffix = \.jat$ 6 | 7 | # keywords 8 | public = public 9 | static = static 10 | atomic = atomic 11 | 12 | annotation = @ {name}( \( {exprlist} \))? 13 | 14 | dquot = "([^"\\]|\\.)*" 15 | squot = '([^'\\]|\\.)*' 16 | num = -?[0-9]+(\.[0-9]+)?(e[+-]?[0-9]+)? 17 | name = (?!(return|for|if|final|static|public)\b)[a-zA-Z][a-zA-Z0-9_]* 18 | chain = ( \. {name} \( {exprlist} \))+ 19 | dotname = {name}( \. {name})* 20 | call = {dotname} \( {exprlist} \) 21 | paren = \( {expr} \) 22 | newxpr = new {type}( \( {exprlist} \)| \{ {exprlist} \}) 23 | indexpr = {dotname} \[ {expr} \] 24 | val=({newxpr}|{indexpr}|{call}|{num}|{dquot}|{squot}|{dotname}|{paren}){chain}? 25 | mulop = [*/%] 26 | aexpr = {val}( {mulop} {val})* 27 | addop = [-+] 28 | shiftexpr = {aexpr}( {addop} {aexpr})* 29 | shiftop = (<<|>>|>>>) 30 | condexpr = {shiftexpr}( {shiftop} {shiftexpr})* 31 | condop = (<=?|>=?|instanceof) 32 | eqexpr = {condexpr}( {condop} {condexpr})? 33 | eqop = [!=]= 34 | ampexpr = {eqexpr}( {eqop} {eqexpr})? 35 | carexpr = {ampexpr}( & {ampexpr})* 36 | pipeexpr = {carexpr}( \^ {carexpr})* 37 | andexpr = {pipeexpr}( \| {pipeexpr})* 38 | andop = && 39 | expr = {andexpr}( {andop} {andexpr})* 40 | extends = extends {type} 41 | implements = ( implements {type}( , {type})*)? 42 | 43 | array = \[ \] 44 | template = < ({name}( , {name})*)? > 45 | 46 | type = {dotname}( {template})?( {array})* 47 | 48 | argdecl = {type} {name} 49 | argsdecl = ({argdecl}( , {argdecl})*)? 50 | 51 | exprlist = ({expr}( , {expr})*)? 52 | callstmt = {dotname} \( {exprlist} \) ; 53 | ifstmt = if \( {expr} \) {block}( else if \( {expr} \) {block})*( else {block})? 54 | aop = [-+*/]?= 55 | index = \[ {expr} \] 56 | dotindex = {dotname}( {index})? 57 | assign = {dotindex} {aop} {expr} \; 58 | whilestmt = while \( {expr} \) {block} 59 | atomstmt = atomic {block} 60 | forinit = {type} {dotname} = {expr}|{dotname} = {expr} 61 | forincr = {dotname} {aop} {expr} 62 | forstmt = for \( {forinit} ; {expr} ; {forincr} \) {block} 63 | return = return {expr} ;|return ; 64 | syncstmt = sync {block} ; 65 | syncassign = AtomicFuture< {type} > {name} = sync {block} ; 66 | 67 | statement = ({syncassign}|{syncstmt}|{forstmt}|{ifstmt}|{whilestmt}|{atomstmt}|{fdecl}|{assign}|{callstmt}|{return}) 68 | 69 | block = \{( {statement})* \} 70 | 71 | mdecl = {type} {name} \( {argsdecl} \) ({block}|;) 72 | ctor = {name} \( {argsdecl} \) ({block}|;) 73 | 74 | final = final 75 | fdecl = ({final} )?{type} {name} (= {expr})? ; 76 | 77 | cdecl = ({atomic} )?({public} )?({static} )?({mdecl}|{fdecl}|{ctor}) 78 | 79 | package = package {dotname} ; 80 | import = import ({static} )?{dotname} ; 81 | fimport = from {dotname} import( {static})? {name}( , {name})*; 82 | 83 | classtype = class|interface|value 84 | cdecls = \{ ({cdecl} )*\} 85 | class = ^ ({package} )?({import} |{fimport} )*({annotation} )?({public} )?({atomic} )?{classtype} {name}( {extends})?{implements} {cdecls} $ 86 | -------------------------------------------------------------------------------- /src/jat/comp/AtomJat.java: -------------------------------------------------------------------------------- 1 | package jat.comp; 2 | 3 | import edu.lsu.cct.piraha.Group; 4 | import java.io.PrintWriter; 5 | 6 | public class AtomJat { 7 | Jat jat; 8 | PrintWriter pw; 9 | public AtomJat(Jat jat,PrintWriter pw) { 10 | this.jat = jat; 11 | this.pw = pw; 12 | } 13 | public void emit(Group g) { 14 | String pn = g.getPatternName(); 15 | if(pn.equals("block")) { 16 | pw.println("{"); 17 | for(int i=0;i0) pw.print('.'); 58 | emit(g.group(i)); 59 | } 60 | } else if(Jat.eq(pn,"syncstmt")) { 61 | pw.println("__tr__.addTask(new Runnable() {"); 62 | pw.print("public void run() "); 63 | jat.emit(g.group(0)); 64 | pw.println("});"); 65 | } else if(Jat.eq(pn,"syncassign")) { // Need to work on this 66 | pw.print("final AtomicFuture<"); 67 | jat.emit(g.group(0)); 68 | pw.print("> "); 69 | jat.emit(g.group(1)); 70 | pw.print("= new AtomicFuture<"); 71 | jat.emit(g.group(0)); 72 | pw.println(">();"); 73 | pw.println("__tr__.addTask(new Runnable() {"); 74 | pw.println("public void run() {"); 75 | jat.emit(g.group(1)); 76 | pw.print(".set("); 77 | pw.print("new java.util.concurrent.Callable<"); 78 | jat.emit(g.group(0)); 79 | pw.println(">() {"); 80 | pw.print("public "); 81 | jat.emit(g.group(0)); 82 | pw.print("call()"); 83 | jat.emit(g.group(2)); 84 | pw.println("}.call(),__tr__);"); 85 | pw.println("}"); 86 | pw.println("});"); 87 | } else { 88 | g.dumpMatches(); 89 | System.out.println(g.substring()); 90 | throw new Error("not handled '"+pn+"'"); 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/jat/test/Test.java: -------------------------------------------------------------------------------- 1 | package jat.test; 2 | import java.io.IOException; 3 | import java.io.File; 4 | import jat.lang.AtomicFuture; 5 | public class Test extends jat.lang.UnitTest { 6 | jat.lang.ValueArray va =new jat.lang.ValueArray(new java.lang.Integer[]{1 ,2 ,3 }); 7 | static final int vv =3 ; 8 | jat.lang.Atom ai = new jat.lang.Atom(); 9 | jat.lang.Atom aj =new jat.lang.Atom(4 ); 10 | jat.lang.Value v =new jat.test.Person(); 11 | int k ; 12 | int k2 =4 + 2 * (3 - 1 ); 13 | jat.test.Test foo (){ 14 | return this ; 15 | } 16 | public Test (){ 17 | /**begin atomic**/ 18 | while(true) { 19 | try { 20 | final jat.lang.Transaction __tr__= new jat.lang.Transaction(); 21 | ai.set(3,__tr__); 22 | __tr__.finish(); 23 | break; 24 | } catch(jat.lang.TransactionRestart __res__) { 25 | continue; 26 | } 27 | } 28 | /**end atomic**/ 29 | } 30 | public void runTest (){ 31 | System .out .println ("Hello, world" ); 32 | for(int i =0 ;i < 10 ;i += 1 ) { 33 | System .out .println ("i=" + i ); 34 | } 35 | final int nn =3 + 1 ; 36 | int n =nn ; 37 | int i ; 38 | for(i =0 ;i < n ;i += 1 ) { 39 | System .out .println ("i=" + i ); 40 | } 41 | if(n < nn ){ 42 | assertTrue (false ,"n < " + nn ); 43 | } 44 | else if(jat.lang.Util.equals(n ,nn )){ 45 | System .out .println ("n == " + nn ); 46 | } 47 | else{ 48 | assertTrue (false ,"n > " + nn ); 49 | } 50 | while(n > 0 ){ 51 | n -= 1 ; 52 | } 53 | assertTrue (jat.lang.Util.equals(n ,0 ),"n == null" ); 54 | jat.test.Test t =new jat.test.Test(); 55 | jat.test.Test t2 =new jat.test.Test().foo ().foo (); 56 | java.lang.Integer n1 =3 ; 57 | java.lang.Double n2 =3.0 ; 58 | assertTrue (jat.lang.Util.equals(n1 ,n2 ),"n1 == n2" ); 59 | java.lang.String s1 ="foo" ; 60 | java.lang.String s2 =s1 + "" ; 61 | assertTrue (jat.lang.Util.equals(s1 ,s2 ),"s1 == s2" ); 62 | jat.test.Test test =new jat.test.Test(); 63 | final jat.lang.AtomicFuture ss =new jat.lang.AtomicFuture(); 64 | /**begin atomic**/ 65 | while(true) { 66 | try { 67 | final jat.lang.Transaction __tr__= new jat.lang.Transaction(); 68 | test.ai.set(test.ai.get(__tr__)+2,__tr__); 69 | ss.set(test.ai.get(__tr__),__tr__); 70 | final AtomicFuture s = new AtomicFuture(); 71 | __tr__.addTask(new Runnable() { 72 | public void run() { 73 | s .set(new java.util.concurrent.Callable() { 74 | public java.lang.String call(){ 75 | return "fish" ; 76 | } 77 | }.call(),__tr__); 78 | } 79 | }); 80 | __tr__.addTask(new Runnable() { 81 | public void run() { 82 | System .out .println ("sstran=" + ss .get ()); 83 | } 84 | }); 85 | __tr__.addTask(new Runnable() { 86 | public void run() { 87 | System .out .println ("stran=" + s .get ()); 88 | } 89 | }); 90 | __tr__.finish(); 91 | break; 92 | } catch(jat.lang.TransactionRestart __res__) { 93 | continue; 94 | } 95 | } 96 | /**end atomic**/ 97 | System .out .println ("ss=" + ss .get ()); 98 | assertTrue (jat.lang.Util.equals(ss .get (),5 ),"ss.get()==5" ); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/jat/ant/Jat.java: -------------------------------------------------------------------------------- 1 | package jat.ant; 2 | 3 | import org.apache.tools.ant.Project; 4 | import org.apache.tools.ant.Task; 5 | import org.apache.tools.ant.BuildException; 6 | import org.apache.tools.ant.types.FileSet; 7 | import org.apache.tools.ant.types.Path; 8 | import org.apache.tools.ant.types.CommandlineJava; 9 | import org.apache.tools.ant.types.Resource; 10 | import org.apache.tools.ant.types.Assertions; 11 | import org.apache.tools.ant.taskdefs.ExecuteJava; 12 | import org.apache.tools.ant.taskdefs.Redirector; 13 | import org.apache.tools.ant.taskdefs.Javac; 14 | import org.apache.tools.ant.taskdefs.Java; 15 | 16 | import java.io.File; 17 | import java.io.FileWriter; 18 | import java.io.IOException; 19 | 20 | import java.util.Iterator; 21 | import java.util.Map; 22 | import java.util.HashMap; 23 | import java.util.Enumeration; 24 | 25 | import java.lang.reflect.Method; 26 | 27 | import jat.lang.UnitTest; 28 | 29 | import java.util.zip.ZipEntry; 30 | import java.util.zip.ZipFile; 31 | 32 | public class Jat extends Task { 33 | Map classFiles = new HashMap(); 34 | String currentJat; 35 | 36 | public void handleOutput(String s) { 37 | String pre = "class: "; 38 | if(s.startsWith(pre)) { 39 | String classFile = s.substring(pre.length()).trim(); 40 | classFiles.put(currentJat,classFile); 41 | } else { 42 | log(s, Project.MSG_INFO); 43 | } 44 | } 45 | static class JatJavac extends Javac { 46 | JatJavac() { 47 | super(); 48 | init(); 49 | } 50 | public void compile(File file) { 51 | compileList = new File[]{file}; 52 | super.compile(); 53 | } 54 | } 55 | public void javac(final String file) throws BuildException { 56 | final String src = file.substring(0,file.length()-4)+".java"; 57 | final File srcFile = new File(src); 58 | if(!srcFile.exists()) { 59 | return; 60 | } 61 | JatJavac javac = new JatJavac(); 62 | javac.setTaskName("javac"); 63 | javac.setClasspath(cp); 64 | javac.setProject(getProject()); 65 | javac.setDebug(true); 66 | javac.setDestdir(new File(".")); 67 | javac.createSrc(); 68 | javac.compile(srcFile); 69 | } 70 | 71 | File find(String needle) throws IOException { 72 | for(String path : cp.toString().split(File.pathSeparator)) { 73 | File p = new File(path); 74 | if(p.isDirectory()) { 75 | File cf = new File(p,needle); 76 | if(cf.exists()) { 77 | return cf; 78 | } 79 | } else { 80 | ZipFile zf = new ZipFile(p); 81 | Enumeration e = zf.entries(); 82 | while(e.hasMoreElements()) { 83 | ZipEntry ze = e.nextElement(); 84 | if(ze.getName().equals(needle)) { 85 | return p; 86 | } 87 | } 88 | } 89 | } 90 | throw new BuildException("Cannot find "+needle); 91 | } 92 | 93 | /** Translate the Jat file to a Java file. */ 94 | public void build(String file) { 95 | try { 96 | currentJat = file; 97 | File javaFile = new File( 98 | file.substring(0,file.length()-3)+"java"); 99 | File jatFile = new File(file); 100 | File jatAntClass = find("jat/ant/Jat.class"); 101 | File jatCompClass = find("jat/comp/Jat.class"); 102 | 103 | // Determine whether the translated file 104 | // is out of date. 105 | boolean upToDate = true; 106 | if(!javaFile.exists()) { 107 | upToDate = false; 108 | } else if(javaFile.lastModified() < jatFile.lastModified()) { 109 | upToDate = false; 110 | } else if(javaFile.lastModified() < jatAntClass.lastModified()) { 111 | upToDate = false; 112 | } else if(javaFile.lastModified() < jatCompClass.lastModified()) { 113 | upToDate = false; 114 | } 115 | if(upToDate) 116 | return; 117 | 118 | System.out.println("compile: "+file); 119 | 120 | Redirector redirector = new Redirector(this); 121 | CommandlineJava command = new CommandlineJava(); 122 | command.createArgument().setValue(file); 123 | command.createVmArgument().setLine("-ea"); 124 | command.createClasspath(getProject()); 125 | command.setClassname("jat.comp.Jat"); 126 | ExecuteJava exe = new ExecuteJava(); 127 | exe.setClasspath(cp); 128 | exe.setJavaCommand(command.getJavaCommand()); 129 | exe.setSystemProperties(command.getSystemProperties()); 130 | redirector.createStreams(); 131 | exe.execute(getProject()); 132 | redirector.complete(); 133 | if (exe.killedProcess()) { 134 | throw new BuildException("Timeout"); 135 | } else { 136 | javac(file); 137 | } 138 | } catch(IOException ioe) { 139 | throw new BuildException("Error: "+ioe.getMessage()); 140 | } 141 | } 142 | FileSet fs; 143 | public void addFileset(FileSet fs) { 144 | this.fs = fs; 145 | } 146 | Path cp; 147 | public void addClasspath(Path cp) { 148 | this.cp = cp; 149 | } 150 | public void init() { 151 | } 152 | @SuppressWarnings("unchecked") 153 | public void execute() { 154 | // There's no way to make the warning go away 155 | Iterator iter = fs.iterator(); 156 | while(iter.hasNext()) { 157 | Resource fr = iter.next(); 158 | build(fr.toString()); 159 | } 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /src/jat/comp/Type.java: -------------------------------------------------------------------------------- 1 | package jat.comp; 2 | 3 | import edu.lsu.cct.piraha.*; 4 | import java.util.*; 5 | import java.io.File; 6 | 7 | public class Type { 8 | boolean isPrimitive; 9 | boolean isInterface = false; 10 | boolean pseudoPrimitive; 11 | String name; 12 | String pkg; 13 | Location location; 14 | boolean isArray; 15 | Set templates = new HashSet(); 16 | private Set interfaces; 17 | 18 | public void check(Jat jat) { 19 | if(pkg.equals("jat.lang")) { 20 | if(name.equals("Atom")) { 21 | for(Type t : templates) { 22 | if(t.isPrimitive) { 23 | ; 24 | } else if(t.pseudoPrimitive) { 25 | ; 26 | } else if(t.isa(ValueType,jat)) { 27 | ; 28 | } else if(location != null) { 29 | throw new CompFailException("jat.lang.Atom cannot contain '"+t+"'",location.srcFile,location.line); 30 | } else { 31 | throw new CompFailException("jat.lang.Atom cannot contain '"+t+"'"); 32 | } 33 | } 34 | } 35 | } 36 | } 37 | 38 | Set getInterfaces(Jat jat) { 39 | if(interfaces != null) 40 | return interfaces; 41 | interfaces = new HashSet(); 42 | try { 43 | Class c = Class.forName(pkg+"."+name); 44 | for(Class ifc : c.getInterfaces()) { 45 | interfaces.add(mkType(ifc,jat)); 46 | } 47 | } catch(Exception ex) { 48 | File src = new File(jat.srcFile); 49 | File alt = new File(src.getParent(),name+".jat"); 50 | Jat jalt = new Jat(jat); 51 | try { 52 | jalt.compile(alt.getAbsolutePath(),false); 53 | interfaces = jalt.interfaces; 54 | } catch(java.io.IOException ioe) {} 55 | } 56 | return interfaces; 57 | } 58 | 59 | public static Type mkType(Class c,Jat jat) { 60 | Type t = new Type(); 61 | t.pkg = c.getPackage().getName(); 62 | t.name = c.getName(); 63 | int n = t.name.lastIndexOf('.'); 64 | if(n >= 0) t.name = t.name.substring(n+1); 65 | t.isInterface = c.isInterface(); 66 | t.isArray = c.isArray(); 67 | t.check(jat); 68 | return t.pseudo(); 69 | } 70 | 71 | private Type(String pkg,String name) { 72 | this.name = name; 73 | this.pkg = pkg; 74 | pseudo(); 75 | } 76 | private Type() {} 77 | static Type mkType(String s,Jat jat) { 78 | Matcher m = jat.g.matcher("type",s); 79 | if(m.matches()) { 80 | return mkType(m,jat); 81 | } else { 82 | throw new Error("Not a type: "+s); 83 | } 84 | } 85 | Type pseudo() { 86 | if("java.lang".equals(pkg) && 87 | Jat.eq(name,"String","Integer","Short","Long","Byte", 88 | "Float","Double","Boolean","Character")) { 89 | pseudoPrimitive = true; 90 | } 91 | return this; 92 | } 93 | boolean isa(Type t,Jat jat) { 94 | if(t.equals(this)) 95 | return true; 96 | for(Type ifc : getInterfaces(jat)) { 97 | if(ifc.isa(t,jat)) 98 | return true; 99 | } 100 | return false; 101 | } 102 | static Type mkType(Group g,Jat jat) { 103 | String pn = g.getPatternName(); 104 | if(Jat.eq(pn,"dotname")) { 105 | if(g.groupCount()==1) { 106 | return mkType(g.group(0),jat); 107 | } else { 108 | Type t = new Type(); 109 | t.location = new Location(jat.srcFile,g.getLine()); 110 | StringBuffer pkg = new StringBuffer(); 111 | for(int i=0;i+1 0) pkg.append('.'); 113 | pkg.append(g.group(i).substring()); 114 | } 115 | t.name = g.group(g.groupCount()-1).substring(); 116 | if(pkg.length()>0) { 117 | t.pkg = pkg.toString(); 118 | } else { 119 | t.pkg = getPackage(t.name,jat); 120 | } 121 | t.check(jat); 122 | return t.pseudo(); 123 | } 124 | } else if(Jat.eq(pn,"type")) { 125 | Type t = mkType(g.group(0),jat); 126 | t.location = new Location(jat.srcFile,g.getLine()); 127 | int j = 1; 128 | if(j < g.groupCount()) { 129 | if(Jat.eq(g.group(j),"template")) { 130 | Group tpl = g.group(j); 131 | for(int i=0;i c = null; 174 | try { 175 | c = Class.forName(look+"."+name); 176 | ret = c.getPackage().getName(); 177 | jat.pkgs.put(name,ret); 178 | return ret; 179 | } catch(Exception ex) {} 180 | } 181 | File f = new File(jat.srcFile); 182 | File alt = new File(f.getParentFile(),name+".jat"); 183 | if(alt.exists()) { 184 | ret = jat.pkg; 185 | jat.pkgs.put(name,ret); 186 | return ret; 187 | } 188 | throw new CompFailException("No package for class '"+name+"'"); 189 | } 190 | 191 | String rep; 192 | 193 | @Override 194 | public int hashCode() { 195 | if(rep == null) 196 | toString(); 197 | return rep.hashCode(); 198 | } 199 | 200 | @Override 201 | public boolean equals(Object o) { 202 | if(o instanceof Type) { 203 | return o.toString().equals(toString()); 204 | } else { 205 | return false; 206 | } 207 | } 208 | 209 | @Override 210 | public String toString() { 211 | if(rep != null) 212 | return rep; 213 | StringBuffer sb = new StringBuffer(); 214 | if(!pkg.equals("")) { 215 | sb.append(pkg); 216 | sb.append('.'); 217 | } 218 | sb.append(name); 219 | if(templates.size()>0) { 220 | sb.append('<'); 221 | int i=0; 222 | for(Type t : templates) { 223 | if(i++ > 0) sb.append(','); 224 | sb.append(t.objectify()); 225 | } 226 | sb.append('>'); 227 | } 228 | if(isArray) { 229 | sb.append("[]"); 230 | } 231 | return rep = sb.toString(); 232 | } 233 | 234 | final static Type IntegerType = new Type("java.lang","Integer"); 235 | final static Type ShortType = new Type("java.lang","Short"); 236 | final static Type FloatType = new Type("java.lang","Float"); 237 | final static Type DoubleType = new Type("java.lang","Double"); 238 | final static Type CharType = new Type("java.lang","Character"); 239 | final static Type ByteType = new Type("java.lang","Byte"); 240 | final static Type BooleanType = new Type("java.lang","Boolean"); 241 | final static Type LongType = new Type("java.lang","Long"); 242 | 243 | final static Type ValueType = new Type("jat.lang","Value"); 244 | final static Type AtomicType = new Type("jat.lang","AtomicClass"); 245 | final static Type ObjectType = new Type("java.lang","Object"); 246 | 247 | public Type objectify() { 248 | if(isPrimitive) { 249 | String tn = name; 250 | if("int".equals(tn)) { 251 | return IntegerType; 252 | } else if("short".equals(tn)) { 253 | return ShortType; 254 | } else if("float".equals(tn)) { 255 | return FloatType; 256 | } else if("double".equals(tn)) { 257 | return DoubleType; 258 | } else if("char".equals(tn)) { 259 | return CharType; 260 | } else if("byte".equals(tn)) { 261 | return ByteType; 262 | } else if("boolean".equals(tn)) { 263 | return BooleanType; 264 | } else if("long".equals(tn)) { 265 | return LongType; 266 | } else { 267 | throw new CompFailException("Not a primitive type "+tn); 268 | } 269 | } 270 | return this; 271 | } 272 | public boolean checkValue(Jat jat) { 273 | if(isArray) return false; 274 | if(isPrimitive) return true; 275 | if(pseudoPrimitive) return true; 276 | return isa(ValueType,jat); 277 | } 278 | } 279 | -------------------------------------------------------------------------------- /src/jat/comp/Jat.java: -------------------------------------------------------------------------------- 1 | package jat.comp; 2 | 3 | import java.io.*; 4 | import edu.lsu.cct.piraha.*; 5 | import java.util.*; 6 | 7 | /* 8 | * Todo: 9 | * (1) atomic class, can only have atomic variables 10 | * or values as fields. Atomic type can only 11 | * hold values or atomic classes. All fields 12 | * final. 13 | * (2) values have an equals method, all final 14 | * fields. All fields are primitives or other 15 | * values. 16 | * (3) static variables can only be final and can 17 | * only be atomic or value. 18 | * (4) asyncs instead of threads 19 | */ 20 | // TODO: Threads must be atomic or value 21 | // objects, but the programmer must pick 22 | // one. Maybe just use async() blocks? 23 | public class Jat { 24 | /// Start Meta-Data 25 | public String pkg; 26 | public String objType; 27 | public String objName; 28 | public String srcFile; 29 | public boolean isAtomic = false; 30 | 31 | public Set interfaces = new HashSet(); 32 | public Type extend = Type.ObjectType; 33 | 34 | /** what package does a class live in? */ 35 | public static Map pkgs = new HashMap(); 36 | 37 | /** what class does a static method/field live in */ 38 | public Map members = new HashMap(); 39 | 40 | public Map fields = new HashMap(); 41 | 42 | public String compFailMsg; 43 | public int compFailLine; 44 | /// End Meta-Data 45 | 46 | boolean atomField; 47 | boolean staticField; 48 | 49 | PrintWriter pw; 50 | Grammar g = new Grammar(); 51 | 52 | public Jat(Jat jat) { 53 | g = jat.g; 54 | } 55 | public Jat() throws IOException { 56 | g.compileFile(new File("src/jat/comp/Jat.peg")); 57 | pkgs.put("System","java.lang"); 58 | pkgs.put("Math","java.lang"); 59 | pkgs.put("String","java.lang"); 60 | pkgs.put("Runnable","java.lang"); 61 | pkgs.put("Exception","java.lang"); 62 | pkgs.put("RuntimeException","java.lang"); 63 | pkgs.put("Error","java.lang"); 64 | pkgs.put("Object","java.lang"); 65 | 66 | pkgs.put("Integer","java.lang"); 67 | pkgs.put("Double","java.lang"); 68 | pkgs.put("Short","java.lang"); 69 | pkgs.put("Long","java.lang"); 70 | 71 | pkgs.put("Byte","java.lang"); 72 | pkgs.put("Character","java.lang"); 73 | pkgs.put("Boolean","java.lang"); 74 | pkgs.put("Float","java.lang"); 75 | 76 | pkgs.put("Value","jat.lang"); 77 | pkgs.put("Atomic","jat.lang"); 78 | pkgs.put("SnapShot","java.lang"); 79 | } 80 | 81 | public static boolean eq(String nm,String... sl) { 82 | for(String s : sl) { 83 | if(nm.equals(s)) 84 | return true; 85 | } 86 | return false; 87 | } 88 | 89 | void emitObj(Group g) { 90 | String s = g.substring(); 91 | if(s.equals("int")) 92 | pw.print("Integer "); 93 | else if(s.equals("short")) 94 | pw.print("Short "); 95 | else if(s.equals("double")) 96 | pw.print("Double "); 97 | else if(s.equals("boolean")) 98 | pw.print("Boolean "); 99 | else if(s.equals("char")) 100 | pw.print("Character "); 101 | else if(s.equals("float")) 102 | pw.print("Float"); 103 | else if(s.equals("long")) 104 | pw.print("Long"); 105 | else if(s.equals("byte")) 106 | pw.print("Byte"); 107 | else 108 | emit(g); 109 | } 110 | public static boolean eq(Group g,String... sl) { 111 | return eq(g.getPatternName(),sl); 112 | } 113 | 114 | void emit(Group g) { 115 | String pn = g.getPatternName(); 116 | if(eq(pn,"classtype")) { 117 | objType = g.substring(); 118 | if(eq(objType,"value")) 119 | pw.print("class"); 120 | else 121 | pw.print(objType); 122 | pw.print(' '); 123 | } else if(eq(pn,"extends")) { 124 | extend = Type.mkType(g.group(0),this); 125 | if(extend.isa(Type.ValueType,this)) { 126 | if(objType.equals("value")) { 127 | ; 128 | } else { 129 | raiseFail( 130 | "Cannot extend a value with a class", 131 | srcFile,g.getLine()); 132 | } 133 | } 134 | if(extend.isa(Type.AtomicType,this)) { 135 | if(!isAtomic) { 136 | raiseFail( 137 | "Cannot extend an atomic class with a class or value", 138 | srcFile,g.getLine()); 139 | } 140 | } 141 | pw.print("extends "); 142 | pw.print(extend); 143 | pw.print(' '); 144 | } else if(eq(pn,"implements")) { 145 | for(int i=0;i 0) pw.print('.'); 169 | emit(g.group(i)); 170 | } 171 | } else if(eq(pn,"argsdecl","exprlist")) { 172 | pw.print('('); 173 | for(int i=0;i 0) pw.print(','); 175 | emit(g.group(i)); 176 | } 177 | pw.print(')'); 178 | } else if(eq(pn,"template")) { 179 | pw.print('<'); 180 | for(int i=0;i0) pw.print(','); 182 | emit(g.group(i)); 183 | } 184 | pw.print('>'); 185 | } else if(eq(pn,"import")) { 186 | int i = 0; 187 | String pre = "import "; 188 | if(g.group(i).getPatternName().equals("static")) { 189 | pre = "import static "; 190 | i++; 191 | } 192 | Group dot = g.group(i); 193 | StringBuffer pkg = new StringBuffer(); 194 | for(int j=0;j+1 0) pkg.append('.'); 196 | pkg.append(dot.group(j).substring()); 197 | } 198 | String cname = dot.group(dot.groupCount()-1).substring(); 199 | String pname = pkg.toString(); 200 | if(i == 0) 201 | pkgs.put(cname,pname); 202 | else 203 | members.put(cname,pname); 204 | pw.println(pre+pname+"."+cname+";"); 205 | } else if(eq(pn,"fimport")) { 206 | int i=0; 207 | String pre = "import "; 208 | if(g.group(i).getPatternName().equals("static")) { 209 | pre = "import static "; 210 | i++; 211 | } 212 | String pname = g.group(i).substring(); 213 | for(int j=i+1;j "); 292 | } else { 293 | pw.print(t); 294 | pw.print(' '); 295 | } 296 | emit(g.group(n+1)); 297 | if(g.groupCount()==n+3) { 298 | pw.print('='); 299 | if(atomField) { 300 | pw.print("new jat.lang.Atom<"); 301 | if(!t.checkValue(this) || t.isa(Type.AtomicType,this)) 302 | raiseFail("'"+t.toString()+"' not allowed in Atomic",g.group(n)); 303 | pw.print(t.objectify()); 304 | pw.print(">("); 305 | emit(g.group(n+2)); 306 | pw.print(')'); 307 | } else { 308 | emit(g.group(n+2)); 309 | } 310 | } else if(atomField) { 311 | pw.print("= new jat.lang.Atom()"); 316 | } 317 | pw.println(';'); 318 | } else if(eq(pn,"ifstmt")) { 319 | int i; 320 | pw.print("if("); 321 | emit(g.group(0)); 322 | pw.print(")"); 323 | emit(g.group(1)); 324 | for(i=2;i+10) pw.print(','); 434 | emit(g.group(1).group(i)); 435 | } 436 | pw.print('}'); 437 | } else { 438 | emit(g.group(1)); 439 | } 440 | /* 441 | for(int i=1;i= 0) { 499 | if(cfe.line == compFailLine) { 500 | System.out.println("Compilation Fail Confirmed"); 501 | File f = new File(outfile); 502 | f.delete(); 503 | return; 504 | } 505 | } 506 | } 507 | System.out.println("compilation failed \""+cfe.getMessage()+"\""); 508 | pw.close(); 509 | if(output) { 510 | File f = new File(outfile); 511 | f.delete(); 512 | new File(outfile).renameTo(f); 513 | } 514 | throw cfe; 515 | } 516 | } else { 517 | File f = new File(outfile); 518 | f.delete(); 519 | throw new CompFailException(mc.near().toString()); 520 | } 521 | } else { 522 | throw new IOException("Bad source file name "+src+" "+m.near()); 523 | } 524 | } 525 | 526 | public void raiseFail(String msg,Group g) { 527 | raiseFail(msg,srcFile,g.getLine()); 528 | } 529 | public void raiseFail(String msg,String file,int line) { 530 | /* 531 | if(compFailMsg != null) { 532 | if(msg.indexOf(compFailMsg) >= 0) { 533 | if(line == compFailLine) { 534 | System.out.print("Confirmed Compilation Failure: "); 535 | System.out.print(msg); 536 | System.out.print(" at "); 537 | System.out.print(file); 538 | System.out.print(':'); 539 | System.out.println(line); 540 | throw new Success(); 541 | } 542 | } else { 543 | System.out.println("No mach for: "+compFailMsg+":"+compFailLine); 544 | } 545 | } 546 | */ 547 | throw new CompFailException(msg,file,line); 548 | } 549 | 550 | public void async(Group g) { 551 | String pn = g.getPatternName(); 552 | if(eq(pn,"exprlist")) { 553 | pw.print('('); 554 | for(int i=0;i 0) pw.print(','); 556 | async(g.group(i)); 557 | } 558 | pw.print(')'); 559 | } else { 560 | raiseFail("Async does not support '"+pn+"'",g); 561 | } 562 | } 563 | 564 | public static void main(String[] args) throws IOException { 565 | Jat jat = new Jat(); 566 | jat.compile(args[0],true); 567 | } 568 | } 569 | --------------------------------------------------------------------------------