about summary refs log tree commit diff stats
path: root/test_layers
Commit message (Expand)AuthorAgeFilesLines
* 3454Kartik K. Agaram2016-10-061-1/+1
* 3453Kartik K. Agaram2016-10-061-0/+4
* 3451Kartik K. Agaram2016-10-061-1/+1
* 3450Kartik K. Agaram2016-10-061-1/+2
* 3449Kartik K. Agaram2016-10-061-1/+2
* 3350Kartik K. Agaram2016-09-141-0/+2
* 3283Kartik K. Agaram2016-08-291-1/+1
* 3275Kartik K. Agaram2016-08-281-1/+1
* 3029Kartik K. Agaram2016-06-021-0/+7
* 2949 - disable Valgrind on app layer testsKartik K. Agaram2016-05-091-44/+33
* 2948Kartik K. Agaram2016-05-081-0/+1
* 2946Kartik K. Agaram2016-05-081-11/+15
* 2945Kartik K. Agaram2016-05-081-5/+44
* 2944Kartik K. Agaram2016-05-081-3/+8
* 2942 - switch scripts to bashKartik K. Agaram2016-05-081-5/+5
* 2941 - split Travis CI into multiple jobsKartik K. Agaram2016-05-081-0/+32
ht: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
const fs = require('fs');


class Room {
    constructor(name, description, items, animals, connections) {
        this.name = name;
        this.description = description;
        this.items = items;
        this.animals = animals;
        this.connections = connections;
    }
}

class Item {
    constructor(name, description, action) {
        this.name = name;
        this.description = description;
        this.action = action;
    }
}

class Animal {
    constructor(name, description, action) {
        this.name = name;
        this.description = description;
        this.action = action;
    }
}

class Interpreter {
    constructor() {
        this.rooms = {};
        this.currentRoom = null;
        this.history = [];
    }

    parseDSLFile(filePath) {
        const dsl = fs.readFileSync(filePath, 'utf8');
        this.parseDSL(dsl);
    }

    parseDSL(dsl) {
        const lines = dsl.split('\n');
        let currentRoom = null;
        let currentItem = null;
        let currentAnimal = null;

        for (const line of lines) {
            const trimmed = line.trim();

            if (trimmed.startsWith('room')) {
                const name = trimmed.split('"')[1];
                currentRoom = new Room(name);
                this.rooms[name] = currentRoom;
            } else if (trimmed.startsWith('description')) {
                const description = trimmed.split('"')[1];
                if (currentItem) {
                    currentItem.description = description;
                } else if (currentAnimal) {
                    currentAnimal.description = description;
                } else {
                    currentRoom.description = description;
                }
            } else if (trimmed.startsWith('item')) {
                const name = trimmed.split('"')[1];
                currentItem = new Item(name);
                currentRoom.items[name] = currentItem;
            } else if (trimmed.startsWith('animal')) {
                const name = trimmed.split('"')[1];
                currentAnimal = new Animal(name);
                currentRoom.animals[name] = currentAnimal;
            } else if (trimmed.startsWith('action')) {
                const action = trimmed.split('"')[1];
                if (currentItem) {
                    currentItem.action = action;
                } else {
                    currentAnimal.action = action;
                }
            } else if (trimmed.startsWith('connection')) {
                const direction = trimmed.split('"')[1];
                const roomName = trimmed.split('"')[3];
                currentRoom.connections[direction] = roomName;
            } 
        }
    }

    interpretCommand(command) {
        const tokens = command.split(' ');
        const action = tokens[0];
        const object = tokens[1];

        if (action === 'go') {
            this.currentRoom = this.rooms[this.currentRoom.connections[object]];
        } else if (action === 'take') {
            if (this.currentRoom.items[object]) {
                console.log(this.currentRoom.items[object].action);
                this.inventory.push(this.currentRoom.items[object]);
                delete this.currentRoom.items[object];
            }
        } else if (action === 'drop') {
            if (this.inventory.includes(object)) {
                this.currentRoom.items[object] = object;
                this.inventory = this.inventory.filter(item => item !== object);
                console.log(`You dropped the ${object}.`);
            }
        } else if (action === 'examine') {
            if (this.currentRoom.items[object]) {
                console.log(this.currentRoom.items[object].description);
            } else if (this.inventory.includes(object)) {
                console.log(object.description);
            } else {
                console.log(`There is no ${object} here to examine.`);
            }
        } else if (this.currentRoom.animals[object]) {
            console.log(this.currentRoom.animals[object].action);
        }

        this.history.push(command);
    }
}

// Create a new interpreter
const interpreter = new Interpreter();

// Parse the DSL file
interpreter.parseDSLFile(process.argv[2]);

// Start a command line interface
const readline = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
});

readline.on('line', (line) => {
    interpreter.interpretCommand(line);
});