time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

peggyHelpers.js (4849B)


      1 export const infixFunctions = {
      2     "+": "add",
      3     "-": "subtract",
      4     "!=": "unequal",
      5     ".-": "dotSubtract",
      6     ".*": "dotMultiply",
      7     "./": "dotDivide",
      8     ".^": "dotPow",
      9     ".+": "dotAdd",
     10     "*": "multiply",
     11     "/": "divide",
     12     "&&": "and",
     13     "^": "pow",
     14     "<": "smaller",
     15     "<=": "smallerEq",
     16     "==": "equal",
     17     ">": "larger",
     18     ">=": "largerEq",
     19     "||": "or",
     20     to: "to",
     21 };
     22 export const unaryFunctions = {
     23     "-": "unaryMinus",
     24     "!": "not",
     25     ".-": "unaryDotMinus",
     26 };
     27 export function nodeCall(fn, args, location) {
     28     return { type: "Call", fn, args, location };
     29 }
     30 export function makeInfixChain(head, tail, location) {
     31     return tail.reduce((result, [operator, right]) => {
     32         return nodeInfixCall(operator, result, right, location);
     33     }, head);
     34 }
     35 export function nodeInfixCall(op, arg1, arg2, location) {
     36     return {
     37         type: "InfixCall",
     38         op,
     39         args: [arg1, arg2],
     40         location,
     41     };
     42 }
     43 export function nodeUnaryCall(op, arg, location) {
     44     return { type: "UnaryCall", op, arg, location };
     45 }
     46 export function nodePipe(leftArg, fn, rightArgs, location) {
     47     return { type: "Pipe", leftArg, fn, rightArgs, location };
     48 }
     49 export function nodeDotLookup(arg, key, location) {
     50     return { type: "DotLookup", arg, key, location };
     51 }
     52 export function nodeBracketLookup(arg, key, location) {
     53     return { type: "BracketLookup", arg, key, location };
     54 }
     55 export function nodeArray(elements, location) {
     56     return { type: "Array", elements, location };
     57 }
     58 export function nodeDict(elements, location) {
     59     const symbols = {};
     60     for (const element of elements) {
     61         if (element.type === "KeyValue" && element.key.type === "String") {
     62             symbols[element.key.value] = element;
     63         }
     64         else if (element.type === "Identifier") {
     65             symbols[element.value] = element;
     66         }
     67     }
     68     return { type: "Dict", elements, symbols, location };
     69 }
     70 export function nodeUnitValue(value, unit, location) {
     71     return { type: "UnitValue", value, unit, location };
     72 }
     73 export function nodeBlock(statements, location) {
     74     return { type: "Block", statements, location };
     75 }
     76 export function nodeProgram(imports, statements, location) {
     77     const symbols = {};
     78     for (const statement of statements) {
     79         if (statement.type === "LetStatement" ||
     80             statement.type === "DefunStatement") {
     81             symbols[statement.variable.value] = statement;
     82         }
     83     }
     84     return { type: "Program", imports, statements, symbols, location };
     85 }
     86 export function nodeBoolean(value, location) {
     87     return { type: "Boolean", value, location };
     88 }
     89 export function nodeFloat(args, location) {
     90     return { type: "Float", ...args, location };
     91 }
     92 export function nodeIdentifier(value, location) {
     93     return { type: "Identifier", value, location };
     94 }
     95 export function nodeIdentifierWithAnnotation(variable, annotation, location) {
     96     return { type: "IdentifierWithAnnotation", variable, annotation, location };
     97 }
     98 export function nodeKeyValue(key, value, location) {
     99     if (key.type === "Identifier") {
    100         key = {
    101             ...key,
    102             type: "String",
    103         };
    104     }
    105     return { type: "KeyValue", key, value, location };
    106 }
    107 export function nodeLambda(args, body, location, name) {
    108     return { type: "Lambda", args, body, location, name: name?.value };
    109 }
    110 export function nodeLetStatement(variable, value, exported, location) {
    111     const patchedValue = value.type === "Lambda" ? { ...value, name: variable.value } : value;
    112     return {
    113         type: "LetStatement",
    114         variable,
    115         value: patchedValue,
    116         exported,
    117         location,
    118     };
    119 }
    120 export function nodeDefunStatement(variable, value, exported, location) {
    121     return { type: "DefunStatement", variable, value, exported, location };
    122 }
    123 export function nodeString(value, location) {
    124     return { type: "String", value, location };
    125 }
    126 export function nodeTernary(condition, trueExpression, falseExpression, kind, location) {
    127     return {
    128         type: "Ternary",
    129         condition,
    130         trueExpression,
    131         falseExpression,
    132         kind,
    133         location,
    134     };
    135 }
    136 export function nodeVoid(location) {
    137     return { type: "Void", location };
    138 }
    139 export function lineComment(text, location) {
    140     return {
    141         type: "lineComment",
    142         value: text,
    143         location,
    144     };
    145 }
    146 export function blockComment(text, location) {
    147     return {
    148         type: "blockComment",
    149         value: text,
    150         location,
    151     };
    152 }
    153 export function parseEscapeSequence(char, location, error) {
    154     if (char[0] == "'") {
    155         return "'";
    156     }
    157     else {
    158         try {
    159             return JSON.parse(`"\\${char.join("")}"`);
    160         }
    161         catch (e) {
    162             error(`Incorrect escape sequence: ${char.join("")}`, location);
    163         }
    164     }
    165 }
    166 //# sourceMappingURL=peggyHelpers.js.map