jweinst1
8/8/2016 - 5:39 PM

javascript ast model for hill script language

javascript ast model for hill script language

//makes one ASTnode
function astNode(name, arguments){
	return {func:name, args:arguments};
}
//main AST object
var AST = (function(){
	function AST(){
		this.tree = {};
		this.ends = [];
	}
	AST.prototype.visualize = function(){
		console.log(JSON.stringify(this.tree))
	};
	return AST;
})();

var makeAST = function(tokens){
	newargs = [];
	for(var i=0;i<tokens.length;i++){
		if(/[a-zA-Z]+/.test(tokens[i])){
			console.log(newargs.concat(tokens.slice(i+1, tokens.length)))
			return {name:tokens[i], args:newargs.concat(makeAST(tokens.slice(i+1, tokens.length)))};
		}
		else {
			//keeps values which are not function calls
			newargs.push(tokens[i])
		}
	}
	return tokens;
}

exports.makeAST = makeAST;