Skip to content

Commit

Permalink
refactor/fix: pretty-printer (#1018)
Browse files Browse the repository at this point in the history
* refactor: introduce DSL to handle indents, etc.
* fix: precedence levels for expressions
  • Loading branch information
verytactical authored Nov 14, 2024
1 parent 387238c commit 21808e4
Show file tree
Hide file tree
Showing 9 changed files with 1,000 additions and 778 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"test": "jest",
"coverage": "cross-env COVERAGE=true jest",
"release": "yarn clean && yarn build && yarn coverage && yarn release-it --npm.yarn1",
"type": "tsc --noEmit",
"lint": "yarn eslint .",
"lint:schema": "ajv validate -s schemas/configSchema.json -d tact.config.json",
"fmt": "yarn prettier -l -w .",
Expand Down
4 changes: 2 additions & 2 deletions src/generator/writers/writeFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { resolveFuncTupleType } from "./resolveFuncTupleType";
import { ops } from "./ops";
import { freshIdentifier } from "./freshIdentifier";
import { idTextErr, throwInternalCompilerError } from "../../errors";
import { prettyPrintAsmShuffle } from "../../prettyPrinter";
import { ppAsmShuffle } from "../../prettyPrinter";

export function writeCastedExpression(
expression: AstExpression,
Expand Down Expand Up @@ -580,7 +580,7 @@ export function writeFunction(f: FunctionDescription, ctx: WriterContext) {
args: fAst.shuffle.args.map((id) => idOfText(funcIdOf(id))),
};
ctx.asm(
prettyPrintAsmShuffle(asmShuffleEscaped),
ppAsmShuffle(asmShuffleEscaped),
fAst.instructions.join(" "),
);
});
Expand Down
15 changes: 9 additions & 6 deletions src/grammar/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,22 @@ export type AstBouncedMessageType = {
//

export type AstExpression =
| AstExpressionPrimary
| AstOpBinary
| AstOpUnary
| AstFieldAccess
| AstNumber
| AstId
| AstBoolean
| AstConditional;

export type AstExpressionPrimary =
| AstMethodCall
| AstFieldAccess
| AstStaticCall
| AstStructInstance
| AstNumber
| AstBoolean
| AstId
| AstNull
| AstInitOf
| AstString
| AstConditional;
| AstString;

export type AstBinaryOperation =
| "+"
Expand Down
21 changes: 12 additions & 9 deletions src/grammar/grammar.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,22 @@ Tact {
| "+" ExpressionUnary --plus
| "!" ExpressionUnary --not
| "~" ExpressionUnary --bitwiseNot
| ExpressionPrimary
| ExpressionPostfix

// Order is important
ExpressionPrimary = ExpressionUnboxNotNull
ExpressionPostfix = ExpressionUnboxNotNull
| ExpressionMethodCall
| ExpressionFieldAccess
| ExpressionStaticCall
| ExpressionPrimary

ExpressionUnboxNotNull = ExpressionPostfix "!!"

ExpressionFieldAccess = ExpressionPostfix "." id ~"("

ExpressionMethodCall = ExpressionPostfix "." id Arguments

// Order is important
ExpressionPrimary = ExpressionStaticCall
| ExpressionParens
| ExpressionStructInstance
| integerLiteral
Expand All @@ -230,12 +239,6 @@ Tact {

ExpressionParens = "(" Expression ")"

ExpressionUnboxNotNull = ExpressionPrimary "!!"

ExpressionFieldAccess = ExpressionPrimary "." id ~"("

ExpressionMethodCall = ExpressionPrimary "." id Arguments

ExpressionStructInstance = typeId "{" ListOf<StructFieldInitializer, ","> ","? "}"

ExpressionStaticCall = id Arguments
Expand Down
Loading

0 comments on commit 21808e4

Please sign in to comment.