Skip to content

Commit

Permalink
Merge pull request #1 from Tengs-Penkwe/basic
Browse files Browse the repository at this point in the history
Basic Implementation
  • Loading branch information
Tengs Fam committed Feb 2, 2024
2 parents 8d90853 + 91a6412 commit 8f23347
Show file tree
Hide file tree
Showing 35 changed files with 1,419 additions and 43 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Node.js CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 16.x, 18.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: yarn install
- name: Run linter
run: yarn lint
- name: Run prettier check
run: yarn pretty --check
- name: Compile TypeScript
run: yarn build
- name: Run tests with coverage
run: yarn cover

40 changes: 36 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
# See https://intellij-support.jetbrains.com/hc/en-us/articles/206544839-How-to-manage-projects-under-Version-Control-Systems
**/.idea/workspace.xml
**/.idea/usage.statistics.xml
**/.idea/tasks.xml
**/.idea/checkstyle-idea.xml
**/.idea/shelf/

# Node
node_modules/
lang/*
package-lock.json
yarn.lock
yarn-error.log
coverage/
dist/

lang/
yarn.lock

# Persist Dir
data/

# Normal Things
.DS_Store
.env
*.log

#IDE
.idea/
.vscode/

#personal test files
test/resources/queries/focus/
test/resources/queries/failing/
test/resources/queries/orderedQueries/
.nyc_output/

#performance output
*.0x/

#coverage report
coverage/
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tabWidth": 4,
"useTabs": false,
"printWidth": 160,
"bracketSpacing": false
}
70 changes: 70 additions & 0 deletions FarmExpr.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
grammar FarmExpr;

options {
visitor = true;
}

// A program is a bunch of statements
prog: stmt+;

stmt: decl_stmt
| if_stmt
| expr_stmt
| assign_stmt
;

// Declarations
decl_stmt: type NAME ('=' (expr | pairs) )? ';' ;

// Expression, they evaluate to a value
expr_stmt: expr ';' ;

// Assign the evaluated value to a variable
assign_stmt: NAME '=' expr ';' ;

// If statement
if_stmt: 'if' expr block ('else' block)? ;

// Argument to function call
args: expr (',' expr)* ;

pairs: '[' pair (',' pair)* ']' ;
pair: NAME ':' expr ;

block: '{' stmt* '}' ;

// Types
type: 'Num'
| 'Bool'
| 'Farm'
| 'Crop'
;

// Function call
call_expr: NAME '(' args? ')' ;

expr: expr op=('*'|'/') expr
| expr op=('+'|'-') expr
| expr op=( '!=' | '==' | '>=' | '<=' | '<' | '>' ) expr
| call_expr
| BOOL
| INT
| FLOAT
| NAME
| STRING
| '(' expr ')'
;



// Tokens

END : ';' ;
INT : [0-9]+ ;
FLOAT: [0-9]+ '.' [0-9]+ ;
BOOL : 'true' | 'false' ;
STRING: '"' ( ~["\\] | '\\' . )* '"' ;
NAME : [a-zA-Z_][a-zA-Z_0-9]*;
WS : [ \t\r\n]+ -> skip ;
LINE_COMMENT : '//' ~[\r\n]* -> skip ;
3 changes: 3 additions & 0 deletions examples/ex1.frm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1 + 1 * 2; // Expression
Num ab = 1; // Declaration
ab = ab + 1; // Assignement
21 changes: 21 additions & 0 deletions examples/ex2.frm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Farm myFarm = [Name: "myFarm", Area: 1200, GridLength: 10, Polyculture: true, MaxWaterUsage: 1500, Season: "Summer"];
Crop myCrop = [Name: "elderberry", Season: "Summer", Water: 45, Yield: 75, SellPrice: 110];

if PossibleCrop("corn") {
Plant("corn", 5, 10, 8, 12);
} else {
if PossibleCrop("strawberry") {
Plant("strawberry", 5, 10, 8, 12);
}
}

if PossibleCrop("corn", 5, 10, 8, 12) {
Plant("corn", 5, 10, 8, 12);
}

Num cornQuant = CropQuantity("corn");
if cornQuant > 100 {
Plant("corn", cornQuant);
}


1 change: 1 addition & 0 deletions examples/ex3.frm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Farm myFarm = [Name: "myFarm", Area: 1200, GridLength: 10, Polyculture: true, MaxWaterUsage: 1500, Season: "Summer"];
10 changes: 0 additions & 10 deletions lang/FarmExpr.g4

This file was deleted.

63 changes: 39 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
{
"name": "Group12Project1",
"version": "1.0.0",
"main": "index.js",
"repository": "[email protected]:CPSC410-2023W-T2/Group12Project1.git",
"author": "Group",
"license": "MIT",
"type": "module",
"dependencies": {
"antlr4ts": "^0.5.0-alpha.4",
"antlr4ts-cli": "^0.5.0-alpha.4"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"eslint": "^8.56.0",
"typescript": "^5.3.3"
},
"scripts": {
"cover": "nyc --reporter text --reporter html yarn run test",
"build": "tsc && yarn lint",
"lint": "eslint src test --ext .ts",
"fix": "yarn lint --fix",
"start": "ts-node src/Parse.ts"
}
"name": "FarmDSL",
"description": "A simple language for describing farms",
"version": "0.0.1",
"repository": "[email protected]:CPSC410-2023W-T2/Group12Project1.git",
"author": "Group 12: Mark, Syed, Pranipa, Has, and Tengs",
"license": "MIT",
"dependencies": {
"@types/antlr4": "^4.11.6",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.16",
"@types/yargs": "^17.0.32",
"antlr4": "^4.13.1",
"prettier": "^3.2.4",
"yargs": "^17.7.2"
},
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/chai-as-promised": "^7.1.8",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"eslint": "8.31.0",
"mocha": "^10.2.0",
"nyc": "15.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
},
"scripts": {
"cover": "nyc --reporter text --reporter html yarn run test",
"build": "tsc && yarn lint",
"lint": "eslint src test --ext .ts",
"pretty": "prettier --config ./.prettierrc.json --write \"./**/*.ts\"",
"test": "mocha --require ts-node/register --timeout 10000 --extension .spec.ts --recursive test",
"st": "mocha --require ts-node/register --timeout 10000",
"fix": "yarn lint --fix",
"lang": "antlr -Dlanguage=TypeScript FarmExpr.g4 -visitor -o lang",
"start": "ts-node src/Main.ts"
}
}
23 changes: 23 additions & 0 deletions src/Error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export class ParseError extends Error {
constructor(message: string) {
super(message);
}
}

export class ExprError extends Error {
constructor(message: string) {
super(message);
}
}

export class FunctionError extends Error {
constructor(message: string) {
super(message);
}
}

export class VariableError extends Error {
constructor(message: string) {
super(message);
}
}
79 changes: 79 additions & 0 deletions src/Main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import * as fs from "fs";
import yargs from "yargs";
import {Arguments} from "yargs";
import {hideBin} from "yargs/helpers";
import {parseProgram} from "./frontend/Parse";
import {transProgram} from "./frontend/Trans";
import {evalProgram} from "./vm/Eval";
import {runRepl} from "./frontend/Repl";

function executeFile(filename: string, verbose = false) {
const programString = fs.readFileSync(filename, "utf-8");
const parsedProgram = parseProgram(programString, verbose);
const program = transProgram(parsedProgram, verbose);
const result = evalProgram(program);

switch (result.type) {
case "Null":
case "Num":
case "Bool":
case "Farm":
case "Crop":
result.show();
break;
case "String":
default:
throw new Error("Unknown result type: " + result.type);
}
process.exit(0);
}

interface MyArguments extends Arguments {
file?: string;
verbose?: boolean;
execute?: string;
}

yargs(hideBin(process.argv))
.scriptName("Farm DSL")
.usage("$0 [options]")
.command("$0", "The default command", {}, (argv: MyArguments) => {
if (argv.file) {
// Execute file if provided
executeFile(argv.file, argv.verbose);
} else {
// Run the REPL if no file option is provided
runRepl(argv.verbose);
}
})
.option("file", {
alias: "f",
type: "string",
description: "File to execute",
requiresArg: true,
})
.option("verbose", {
alias: "v",
type: "boolean",
description: "Run with verbose logging",
})
.option("execute", {
alias: "e",
type: "string",
describe: "Execute a single line of expression",
requiresArg: true,
})
.help("help")
.alias("help", "h")
.version("version", "0.0.1")
.alias("version", "V")
.showHelpOnFail(true)
.epilog("Author: Group 12")
.check((argv: MyArguments) => {
if (argv.execute) {
parseProgram(argv.execute, true);
process.exit(0);
}
return true;
})
.parse();
1 change: 0 additions & 1 deletion src/Parse.ts

This file was deleted.

21 changes: 21 additions & 0 deletions src/ast/Args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {ASTNode} from "./Ast";
import {Result} from "./Type";
import {Expression} from "./Expression";

export class Args implements ASTNode {
args: Expression[];

constructor() {
this.args = [];
}
addArg(pair: Expression) {
this.args.push(pair);
}
addArgs(pairs: Expression[]) {
this.args.push(...pairs);
}

eval(): Result {
throw new Error("Should not eval Args, it is a list of expressions");
}
}
4 changes: 4 additions & 0 deletions src/ast/Ast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {Result} from "./Type";

Check failure on line 1 in src/ast/Ast.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'Result' is defined but never used

Check failure on line 1 in src/ast/Ast.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'Result' is defined but never used
import {Context} from "../vm/Context";

Check failure on line 2 in src/ast/Ast.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'Context' is defined but never used

Check failure on line 2 in src/ast/Ast.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'Context' is defined but never used

export interface ASTNode {}
Loading

0 comments on commit 8f23347

Please sign in to comment.