All Downloads are FREE. Search and download functionalities are using the official Maven repository.

js.prompto.runtime.Interpreter.js Maven / Gradle / Ivy

var DictType = require("../type/DictType").DictType;
var TextType = require("../type/TextType").TextType;
var ArgumentAssignment = require("../grammar/ArgumentAssignment").ArgumentAssignment;
var ArgumentAssignmentList = require("../grammar/ArgumentAssignmentList").ArgumentAssignmentList;
var UnresolvedArgument = require("../argument/UnresolvedArgument").UnresolvedArgument;
var Identifier = require("../grammar/Identifier").Identifier;
var DictLiteral = require("../literal/DictLiteral").DictLiteral;
var MethodCall = require("../statement/MethodCall").MethodCall;
var MethodSelector = require("../expression/MethodSelector").MethodSelector;
var CmdLineParser = require("../utils/CmdLineParser").CmdLineParser;
var Dictionary = require("../value/Dictionary").Dictionary;
var ExpressionValue = require("../value/ExpressionValue").ExpressionValue;

var argsType = new DictType(TextType.instance);

function parseCmdLineArgs(cmdLineArgs) {
	try {
		var args = CmdLineParser.parse(cmdLineArgs);
		var valueArgs = {};
		Object.keys(args).forEach(function(s) {
            var key = new Text(s);
			var value = new Text(args[s]);
			valueArgs[key] = value;
		});
		var dict = new Dictionary(TextType.instance, valueArgs, false);
		return new ExpressionValue(argsType, dict);
	} catch(e) {
		// TODO
		return new DictLiteral(false);
	}
}

function buildAssignments(method, cmdLineArgs) {
	var assignments = new ArgumentAssignmentList();
	if(method.args.length==1) {
		var id = method.args[0].id;
		var value = parseCmdLineArgs(cmdLineArgs);
		assignments.add(new ArgumentAssignment(new UnresolvedArgument(id), value));
	}
	return assignments;
}


function locateMethod(context, methodName, cmdLineArgs) {
	var map = context.getRegisteredDeclaration(methodName);
	if(map==null) {
		throw new SyntaxError("Could not find a \"" + methodName + "\" method.");
	}
	return locateMethodInMap(map, cmdLineArgs);
}

function locateMethodInMap(map, cmdLineArgs) {
	if(cmdLineArgs==null || cmdLineArgs.length==0) {
		return locateMethodWithArgs(map);
	} else {
		return locateMethodWithArgs(map, new DictType(TextType.instance));
	}
}

function locateMethodWithArgs(map) {
    var protos = Object.keys(map.protos);
	// try exact match first
    for(var i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy