Archive

Archive for the ‘jas-blocks’ Category

AS3 :: ASBlocks :: Java impl IStatementContainer example

July 29th, 2011 Michael Schmalle 6 comments

Hi,

Just wanted to show you where the Java impl DOM is at for jas-blocks (Java ActionScript Blocks). This framework reads and writes ActionScript3 from Java applications.

The Java source code to create the output below;

ASBlocks Wiki Labs for a bunch of documentation. I am always adding to this as I get deeper into this framework.

Feel free to ask any questions or let me know if you are interested in some near future beta testing.

public static void createClassStatements(IASProject project)
{
	IASCompilationUnit unit = project.newClass("foo.bar.ClassStatements");
 
	IASPackage pckg = (IASPackage) unit.getPackage();
	IASClassType ctype = (IASClassType) unit.getType();
	IASMethod method = ctype.newMethod("statements", Visibility.PUBLIC, "void");
 
	// IASBreakStatement
	IASBreakStatement bs = method.newBreak();
	bs = method.newBreak("foo");
 
	// IASConfigStatement
	IASConfigStatement config = method.newConfig(factory.newExpression("debug"));
	config.addComment(" debug configuration");
 
	// IASContinueStatement
	IASContinueStatement cs = method.newContinue();
	cs = method.newContinue("bar");
 
	//TODO IASDeclarationStatement
	IASDeclarationStatement ds = method.newDeclaration(factory.newExpression("i:int = 0"));
 
	// IASDefaultXMLNamespaceStatement
	IASDefaultXMLNamespaceStatement dns = method.newDefaultXMLNamespace("http://foo.bar");
 
	// IASDoWhileStatement
	IASDoWhileStatement dws = method.newDoWhile(factory.newExpression("a < b"));
	dws.addComment(" do while()");
 
	// IASExpressionStatement
	IASExpressionStatement es = method.newExpressionStatement(factory.newExpression("trace('expression statement')"));
 
	// IASForStatement
	IASForStatement fs = method.newFor(null, null, null);
	fs.addComment(" for statement uninitialized");
	fs = method.newFor(factory.newDeclaration("i:int = 0"), factory.newExpression("i < len"), factory.newExpression("i++"));
	fs.addComment(" for statement initialized");
 
	// IASForEachInStatement
	IASForEachInStatement fei = method.newForEachIn(factory.newDeclaration("obj:Object"), factory.newExpression("obj[foo]"));
	fei.addComment(" for each in statement");
 
	// IASForInStatement
	IASForInStatement fi = method.newForIn(factory.newDeclaration("obj:Object"), factory.newExpression("obj[foo][0]"));
	fi.addComment(" for in statement");
 
	// IASIfStatement
	IASIfStatement ifs = method.newIf(factory.newExpression("!foo"));
	ifs.addComment(" if statement");
	ifs.getElse().addComment("else statement");
 
	// IASLabelStatement
	IASLabelStatement ls = method.newLabel(factory.newExpression("foo"));
	ls = method.newLabel(factory.newExpression("bar"));
	//TODO figure out the api for this
	IASDoWhileStatement lsl = new ASTASDoWhileStatement(ASTStatementBuilder.newDoWhileAST("true"));
	ls.setStatement(lsl);
 
	// IASReturnStatement
	IASReturnStatement rs = method.newReturn();
	rs = method.newReturn(factory.newExpression("foo()"));
 
	// IASSuperStatement
	// NA
 
	// IASSwitchStatement
	IASSwitchStatement ss = method.newSwitch(factory.newExpression("foo.bar"));
	IASSwitchCase swc = ss.newCase("1");
	swc.addComment(" switch case 1");
	IASSwitchDefault swd = ss.newDefault();
	swd.addComment(" switch default");
 
	// IASThrowStatement
	IASThrowStatement ts = method.newThrow(factory.newExpression("new Error('throw me')"));
	ts = method.newThrow(factory.newExpression("other.error(e)"));
 
	// IASTryStatement
	IASTryStatement trs = method.newTryCatch("e", "Error");
	trs.addComment(" the try block");
	trs.getCatchClauses().get(0).addComment(" the catch block");
	IASFinallyClause fic = trs.newFinallyClause();
	fic.addComment(" the finally clause");
 
	trs = method.newTryFinally();
	trs.addComment(" the try block");
	trs.getFinallyClause().addComment(" the finally block");
	trs.newCatchClause("e1", "Error").addComment(" the catch clause");
 
	// IASWhileStatement
	IASWhileStatement ws = method.newWhile(factory.newExpression("a < b"));
	ws.addComment(" while statement block");
 
	// IASWithStatement
	IASWithStatement wts = method.newWith(factory.newExpression("foo.bar"));
	wts.addComment(" with statement body");
}

ActionScript3 output

package foo.bar {
	public class ClassStatements {
		public function statements():void {
			break;
			break foo;
			CONFIG::debug {
				// debug configuration
			}
			continue;
			continue bar;
			var i;
			default xml namespace = "http://foo.bar";
			do {
				// do while()
			} while (a < b);
			trace('expression statement');
			for (; ; ) {
				// for statement uninitialized
			}
			for (var i:int = 0; i < len; i++) {
				// for statement initialized
			}
			for each(var obj:Object in obj[foo]) {
				// for each in statement
			}
			for (var obj:Object in obj[foo][0]) {
				// for in statement
			}
			if (!foo) {
				// if statement
			} else {
				//else statement
			}
			foo: {
			}
			bar: do {
			} while (true);
			return;
			return foo();
			switch (foo.bar) {
				case 1:
					// switch case 1
				default:
					// switch default
			}
			throw new Error('throw me');
			throw other.error(e);
			try {
				// the try block
			} catch (e:Error) {
				// the catch block
			} finally {
				// the finally clause
			}
			try {
				// the try block
			} catch (e1:Error) {
				// the catch clause
			} finally {
				// the finally block
			}
			while (a < b) {
				// while statement block
			}
			with (foo.bar) {
				// with statement body
			}
		}
	}
}

Remember, this is one VERY SMALL piece of the jas-blocks DOM. We are only talking statement container here.

I’m getting so close to an alpha release but, I have been in the business long enough to know I don’t want to put anything out there until I am sure of the DOM.

ASBlocks Wiki Labs for a bunch of documentation. I am always adding to this as I get deeper into this framework.

PS, I have the framework parsing MXML now to! I am releasing an FX Dom under com.teotigraphix.fxblocks. The as3 is all released under the org.as3commons.asblocks package.

Peace,
Mike