Skip to content

Commit

Permalink
Add control flow in interpretCommand, and todos
Browse files Browse the repository at this point in the history
Added a control flow specifying the three cases "take", "put" and "move".
Also created methods to find relations, and a method stub + todos for the
important findObjectFromEnt(...) method.
  • Loading branch information
JonatanKilhamn committed May 11, 2015
1 parent 7fa451d commit 3959278
Showing 1 changed file with 68 additions and 6 deletions.
74 changes: 68 additions & 6 deletions Interpreter.ts
Expand Up @@ -18,11 +18,14 @@ module Interpreter {
} else {
throw new Interpreter.Error("Found no interpretation");
}
}
}




export interface Result extends Parser.Result {intp:Literal[][];}
export interface Literal {pol:boolean; rel:string; args:string[];}



export function interpretationToString(res : Result) : string {
Expand All @@ -49,15 +52,74 @@ module Interpreter {
function interpretCommand(cmd : Parser.Command, state : WorldState) : Literal[][] {
// This returns a dummy interpretation involving two random objects in the world
var objs : string[] = Array.prototype.concat.apply([], state.stacks);
var a = objs[getRandomInt(objs.length)];
var b = objs[getRandomInt(objs.length)];
var intprt : Literal[][] = [[
{pol: true, rel: "ontop", args: [a, "floor"]},
{pol: true, rel: "holding", args: [b]}

var verb : string = cmd.cmd;
var loc : Parser.Location;
var ent : Parser.Entity;

var relation;
var object1;
var object2;
var objects;

if (verb == "take") {
// we shall pick up something and hold it;
ent = cmd.ent;
object1 = findObjectFromEnt(ent);
relation = "holding";
objects = [object1];
} else if (verb == "put") {
// we are holding something, and shall place it somewhere
object1 = whatAreWeHolding(state);
// TODO: error handling if we are not actually holding anything
loc = cmd.loc;
relation = findRelFromLoc(loc);
object2 = findObjFromLoc(loc);
objects = [object1, object2]
} else if (verb == "move") {
// we shall move something
ent = cmd.ent;
object1 = findObjectFromEnt(ent);
loc = cmd.loc;
relation = findRelFromLoc(loc);
object2 = findObjectFromLoc(loc);
objects = [object1, object2]
}

var intprt : Literal[][];
intprt = [[
{pol: true, rel: relation, args: args}
]];
return intprt;
}

function findObjectFromEnt(state : WorldState, ent : Parser.Entity) : string {
var form = ent.form;
if (form == "floor") {
return form;
}
//TODO: quantifiers

//TODO: simple case: object has colour and form

//TODO: complex case: object is described in relation to another object

//TODO: error handling if object can't be found
}

function findRelFromLoc(state : WorldState, loc : Parser.Location) : string {
return loc.rel;
// Possible results: leftof, rightof, ontop, under, above, beside, inside
}

function findObjectFromLoc(state : WorldState, loc : Parser.Location) : string {
var ent : Parser.Entity = loc.ent;
return findObjectFromEnt(state, ent);
}

function whatAreWeHolding(state : WorldState) : string {
return state.holding;
}

function getRandomInt(max) {
return Math.floor(Math.random() * max);
Expand Down

0 comments on commit 3959278

Please sign in to comment.