Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gravlax

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gravlax - npm Package Compare versions

Comparing version 0.10.0 to 0.11.0

lib/lox-class.js

4

lib/ast-printer.js

@@ -14,2 +14,3 @@ import {

call: (expr) => parenthesize(expr.callee, ...expr.args),
class: (stmt) => parenthesizeText("class", stmt.name.lexeme, "..."),
expr: (stmt) => visitExpr(stmt.expression, astPrinter),

@@ -21,2 +22,3 @@ func: (stmt) => parenthesizeText(

),
get: (expr) => parenthesize("get", expr.object, expr.name.lexeme),
grouping: (expr) => parenthesize("group", expr.expr),

@@ -33,2 +35,4 @@ if: (stmt) => parenthesizeText(

return: (stmt) => parenthesize("return", ...stmt.value ? [stmt.value] : []),
set: (expr) => parenthesize("set", expr.object, expr.name.lexeme, expr.value),
this: () => parenthesize("this"),
unary: (expr) => parenthesize(expr.operator.lexeme, expr.right),

@@ -35,0 +39,0 @@ "var-expr": (expr) => String(expr.name.literal),

@@ -7,3 +7,5 @@ import {

import { Environment } from "./environment.js";
import { LoxClass } from "./lox-class.js";
import { LoxFunction } from "./lox-function.js";
import { LoxInstance } from "./lox-instance.js";
import { isCurrency } from "./lox-value.js";

@@ -160,5 +162,22 @@ import { runtimeError } from "./main.js";

}
class(stmt) {
this.#environment.define(stmt.name.lexeme, null);
const methods = /* @__PURE__ */ new Map();
for (const method of stmt.methods) {
const func = new LoxFunction(
method,
this.#environment,
method.name.lexeme === "init"
);
methods.set(method.name.lexeme, func);
}
const klass = new LoxClass(stmt.name.lexeme, methods);
this.#environment.assign(stmt.name, klass);
}
evaluate(expr) {
return visitExpr(expr, this);
}
this(expr) {
return this.#lookUpVariable(expr.keyword, expr);
}
execute(stmt) {

@@ -182,5 +201,12 @@ visitStmt(stmt, this);

func(stmt) {
const func = new LoxFunction(stmt, this.#environment);
const func = new LoxFunction(stmt, this.#environment, false);
this.#environment.define(stmt.name.lexeme, func);
}
get(expr) {
const obj = this.evaluate(expr.object);
if (obj instanceof LoxInstance) {
return obj.get(expr.name);
}
throw new RuntimeError(expr.name, "Only instances have properties.");
}
grouping(expr) {

@@ -227,2 +253,11 @@ return this.evaluate(expr.expr);

}
set(expr) {
const obj = this.evaluate(expr.object);
if (!(obj instanceof LoxInstance)) {
throw new RuntimeError(expr.name, "Only instances have fields.");
}
const value = this.evaluate(expr.value);
obj.set(expr.name, value);
return value;
}
unary(expr) {

@@ -229,0 +264,0 @@ const right = this.evaluate(expr.right);

@@ -5,8 +5,10 @@ import { LoxCallable } from "./callable.js";

class LoxFunction extends LoxCallable {
#isInitializer;
closure;
declaration;
constructor(declaration, closure) {
constructor(declaration, closure, isInitializer) {
super();
this.declaration = declaration;
this.closure = closure;
this.#isInitializer = isInitializer;
}

@@ -16,2 +18,7 @@ arity() {

}
bindThis(instance) {
const env = new Environment(this.closure);
env.define("this", instance);
return new LoxFunction(this.declaration, env, this.#isInitializer);
}
// Why, oh why, do I need type annotations here?

@@ -31,2 +38,5 @@ call(interpreter, args) {

}
if (this.#isInitializer) {
return this.closure.getAt(0, "this");
}
return null;

@@ -33,0 +43,0 @@ }

@@ -59,3 +59,5 @@ import { errorOnToken } from "./main.js";

try {
if (match("fun")) {
if (match("class")) {
return classDecl();
} else if (match("fun")) {
return func("function");

@@ -74,2 +76,12 @@ } else if (match("var")) {

};
const classDecl = () => {
const name = consume("identifier", "Expect class name.");
consume("{", "Expect '{' before class body.");
const methods = [];
while (!check("}") && !isAtEnd()) {
methods.push(func("method"));
}
consume("}", "Expect '}' after class body.");
return { kind: "class", methods, name };
};
const func = (kind) => {

@@ -199,2 +211,4 @@ const name = consume("identifier", `Expect ${kind} name.`);

return { kind: "assign", name, value };
} else if (expr.kind == "get") {
return { ...expr, kind: "set", value };
}

@@ -236,2 +250,5 @@ error(equals, "Invalid assignment target.");

expr = finishCall(expr);
} else if (match(".")) {
const name = consume("identifier", "Expect property name after '.'.");
expr = { kind: "get", name, object: expr };
} else {

@@ -269,2 +286,4 @@ break;

return { expr, kind: "grouping" };
} else if (match("this")) {
return { keyword: previous(), kind: "this" };
} else if (match("identifier")) {

@@ -271,0 +290,0 @@ return { kind: "var-expr", name: previous() };

@@ -9,2 +9,3 @@ import {

let currentFunc = "none";
let currentClass = "none";
const beginScope = () => {

@@ -74,2 +75,18 @@ scopes.push(/* @__PURE__ */ new Map());

},
class(stmt) {
const encClass = currentClass;
currentClass = "class";
declare(stmt.name);
define(stmt.name);
beginScope();
scopes.at(-1)?.set("this", true);
for (const method of stmt.methods) {
resolveFunction(
method,
method.name.lexeme === "init" ? "initializer" : "method"
);
}
endScope();
currentClass = encClass;
},
expr(stmt) {

@@ -83,2 +100,5 @@ resolveExpr(stmt.expression);

},
get(expr) {
resolveExpr(expr.object);
},
grouping(expr) {

@@ -108,5 +128,22 @@ resolveExpr(expr.expr);

if (stmt.value) {
if (currentFunc === "initializer") {
errorOnToken(
stmt.keyword,
"Can't return a value from an initializer."
);
}
resolveExpr(stmt.value);
}
},
set(expr) {
resolveExpr(expr.value);
resolveExpr(expr.object);
},
this(expr) {
if (currentClass === "none") {
errorOnToken(expr.keyword, "Can't use 'this' outside of a class.");
this.return;
}
resolveLocal(expr, expr.keyword);
},
unary(expr) {

@@ -113,0 +150,0 @@ resolveExpr(expr.right);

5

package.json
{
"name": "gravlax",
"version": "0.10.0",
"version": "0.11.0",
"description": "A Lox interpreter with tasty TypeScript seasoning",

@@ -24,2 +24,3 @@ "repository": {

"build": "tsup",
"coverage": "pnpm test -- --coverage --no-watch",
"format": "prettier \"**/*\" --ignore-unknown",

@@ -86,3 +87,3 @@ "lint": "eslint . .*js --max-warnings 0",

},
"packageManager": "pnpm@8.14.1",
"packageManager": "pnpm@8.14.3",
"engines": {

@@ -89,0 +90,0 @@ "node": ">=20"

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc