Coder Social home page Coder Social logo

Comments (19)

marvinhagemeister avatar marvinhagemeister commented on July 20, 2024

I'm not able to reproduce that on my end. Can you share the full example? What values do the variables exporters and importsForV contain?

from deno.

MikeyA-yo avatar MikeyA-yo commented on July 20, 2024

I'm not able to reproduce that on my end. Can you share the full example? What values do the variables exporters and importsForV contain?

They have same element,

exporters = ["no", "tests"]
importsForV = ["no", "tests"]

The whole code is actually preety long and the values are not hardcoded into those arrays, but i log out the values of both of them and it was the same, that's why .every works, i also tried it on node and bun, it also works

Entire file incase needed:

#!/usr/bin/env -S deno run -A

import { objectChecker } from "./error_handling/objectChCompiled.js";

const out = "./out.js"; // Using Deno.cwd() to get current working directory Deno.cwd().slice(2) +
const programName = Deno.args[0]; // Using Deno.args to get command line arguments
// const buf = new Uint8Array(2000)
// await Deno.openSync(programName).read(buf)
const program = Deno.readTextFileSync(programName);

// this function breaks the whole program into lines
function parse(codes: string): string[] {
  return codes.split("\n");
}
// this function breaks a line into words by white space
// function tokenize(code: string): string[] {
//     return code.split(/\s+/);
// }
// this function is peak
function parser(inputString: string) {
  // Separate the input string into segments
  const segments = inputString.match(/(["'`].*?["'`])|\S+/g);

  if (!segments) return [];

  const result: string[] | any = [];

  for (const segment of segments) {
    if (
      segment.startsWith('"') || segment.startsWith("'") ||
      segment.startsWith("`")
    ) {
      // Quoted strings
      result.push(segment);
    } else {
      // Split by parentheses, square brackets, braces, and operators
      const tokens = segment.split(/([()\[\]{}])/).filter((token) =>
        token.trim() !== ""
      );

      // Combine adjacent parentheses, square brackets, or braces
      let combinedToken = "";
      for (const token of tokens) {
        if (
          token === "(" || token === "[" || token === "{" || token === ")" ||
          token === "]" || token === "}"
        ) {
          if (combinedToken !== "") {
            result.push(combinedToken);
            combinedToken = "";
          }
          result.push(token);
        } else {
          combinedToken += token;
        }
      }

      // Push any remaining combined token
      if (combinedToken !== "") {
        result.push(combinedToken);
      }
    }
  }

  return result;
}
function parseStr(inputString: string) {
  const regex = /(["'`])(.*?)\1|\S+/g;
  const matches = inputString.match(regex);

  if (matches) {
    return matches;
  } else {
    return [];
  }
}
// this function breaks a quote statement apart
// function parseStatement(statement): string[] {
//     const regex = /"([^"]+)"|(\w+)|([=\[\]\(\){}รท*+\-])/g; // Matches either a quoted string or a word /("[^"]+"|\w+)/g
//     const matches = statement.match(regex);
//     return matches;
// }
let exporters: string[] = [];
function generateCode(program: any) {
  let code = "";
  let lines = parse(program);
  let newLines = lines.filter((line) => {
    return line.trim() !== "";
  });
  if (newLines[0] == "\\r") {
    newLines[0] = "\n";
  }
  if (newLines[0].includes("#")) {
    newLines[0] = "";
  }

  newLines.forEach((el) => {
    el.includes("{")
      ? (el += "")
      : el.includes(";")
      ? (el += "")
      : el.includes("}")
      ? (el += "")
      : el.includes(",")
      ? (el += "")
      : el.includes(":")
      ? (el += "")
      : el.includes("imp@") ? el += '' : (el += " ;");
    let values: RegExpMatchArray | never[] | string[] = parseStr(el);
    if (
      el.includes("for (") || el.includes("for(") || el.includes("if(") ||
      el.includes("if (") || el.includes("exp@ f")
    ) {
      values = parser(el);
    }
    values[values.length] = "\n";
    for (let i = 0; i < values.length; i++) {
      if (values[i] == "l") {
        values[i] = "let";
      }
      if (values[i] == "print") {
        values[i] = `console.log(${values[i + 1]})`;
        values[i + 1] = " ";
      }
      if (values[i] == "f") {
        values[i] = "function";
      }
      if (values[i] == "exp@") {
        exporters.push(values[i + 2]); // exported variable name to list of exported in a file
        values[i] = "export";
      }
      if (values[i] == "imp@") {
        // let impe = imp(values, values[i]);
        // values[i] = impe
        const impStatementLength = values.length;
        // import for variable
        let importForV = values[i + 1].slice(1, -1);
        const endMark = impStatementLength - 2;
       
        // import location
        let importLocation = values[endMark];
        const impLength = importLocation.length;
        importLocation = importLocation.slice(1, impLength - 1);
        const ayImport = Deno.readTextFileSync(
          `${Deno.cwd()}${importLocation}`,
        );
        //  console.log(values[i],values[impStatementLength - 3])
        //  console.log(importForV,importLocation,values)
        values[i] = "";
        values[i + 1] = "";
        values[2] = "";
        values[endMark] = "";
        if (importForV == importLocation) {
          code += generateCode(ayImport);
        } else {
          //to do actually make sure the file isn't loaded and executed
          let importsForV: string[] = [];
          let condition: any;
          if (importForV.split(",").length > 1) {
            importsForV = importForV.split(",");
            //for some reason every works in deno and some doesn't
            condition = exporters.every((r) => importsForV.includes(r));
            for (let i = 0; i < importsForV.length; i++) {
              if (condition) {
                importForV = importsForV.join(", ");
              }
            }
          }
          const tempCode = generateCode(ayImport);
          if (exporters.includes(importForV) || condition) {
            const math =
              `import {rand, round, PI, floor, exp, degToRad, radToDeg} from './math.ts';\n`;
            const utils =
              `import {print, timer, Day, interval, read, write, appendFile, dirname} from './utils.js';\n`;
            const AY = `import {AY} from './objects/AY.js';\n`;
            const exec = ` ${math} ${utils} ${AY}  ${tempCode} `;
            const out2 = import.meta.dirname + "/out2.js";
            Deno.writeTextFileSync(out2, exec);
            code += `import {${importForV}} from "./out2.js"`;
          }
          if (!exporters.includes(importForV) && !condition) {
            console.log("exports: ", exporters);
            console.log("No exports found", importForV);
            Deno.exit(1);
          }
        }
      }
    }
    // switch case will only be used for error handling
    // switch (values[0]) {
    //     case 'l':
    //         values[0] = 'let';

    //         break;
    //     case 'print':
    //         values[0] = `console.log(${values[1]});`;
    //         values[1] = ' '
    //         break;
    //     case 'f':
    //         values[0] = `function`;
    //         break;
    //     default:
    //       break ;
    // }
    //  values = values.filter(value => value !== undefined || value !== null)
    code += values.join(" ");
  });
  return code;
}
const math =
  `import {rand, round, PI, floor, exp, degToRad, radToDeg} from './math.ts';\n`;
const utils =
  `import {print, timer, Day, interval, read, write, appendFile, dirname} from './utils.js';\n`;
const AY = `import {AY} from './objects/AY.js';\n`;
const some = generateCode(program);
const exec = `${math}${utils}${AY} ${some} `;
objectChecker(some);
Deno.writeTextFileSync(out, exec);
import(out);

from deno.

marvinhagemeister avatar marvinhagemeister commented on July 20, 2024

I'm not able to reproduce it:

const exporters = ["no", "tests"];
const importsForV = ["no", "tests"];

// Logs: true
console.log(exporters.some((r) => importsForV.includes(r)));

// Logs: true
console.log(exporters.every((r) => importsForV.includes(r)));

from deno.

MikeyA-yo avatar MikeyA-yo commented on July 20, 2024

I'm not able to reproduce it:

const exporters = ["no", "tests"];
const importsForV = ["no", "tests"];

// Logs: true
console.log(exporters.some((r) => importsForV.includes(r)));

// Logs: true
console.log(exporters.every((r) => importsForV.includes(r)));

what version of deno are you using, i'll upgrade to that?
Thank you so much by the way

from deno.

marvinhagemeister avatar marvinhagemeister commented on July 20, 2024

Ran a script that tried every version from 1.30.0 to 1.43.6. All of them work. Given that those Array methods are defined inside v8 itself, it seems unlikely that the bug is there. Otherwise the same issue would be present in Node or Chrome. Maybe something else is going on in the code and it fails elsewhere?

from deno.

MikeyA-yo avatar MikeyA-yo commented on July 20, 2024

Ran a script that tried every version from 1.30.0 to 1.43.6. All of them work. Given that those Array methods are defined inside v8 itself, it seems unlikely that the bug is there. Otherwise the same issue would be present in Node or Chrome. Maybe something else is going on in the code and it fails elsewhere?

Yes i was also shocked maybe i'll reinstall deno, also when i check my task manager deno seems to be using up memory, when i'm not running it, what could be the cause

from deno.

MikeyA-yo avatar MikeyA-yo commented on July 20, 2024

Ran a script that tried every version from 1.30.0 to 1.43.6. All of them work. Given that those Array methods are defined inside v8 itself, it seems unlikely that the bug is there. Otherwise the same issue would be present in Node or Chrome. Maybe something else is going on in the code and it fails elsewhere?

So i did some login again and this is exactly what i have as the array values of the 2 elements:

importsForV: [ " no", " tests" ]
exports:  [ "no", "tests" 
]```

it still doesn't work and i just upgraded my version

from deno.

dsherret avatar dsherret commented on July 20, 2024

@MikeyA-yo do you have a repository that shows the reproduction? That code imports ./error_handling/objectChCompiled.js so we might not be able to reproduce it.

from deno.

MikeyA-yo avatar MikeyA-yo commented on July 20, 2024

@MikeyA-yo do you have a repository that shows the reproduction? That code imports ./error_handling/objectChCompiled.js so we might not be able to reproduce it.

This is the full link to the exact code: https://github.com/MikeyA-yo/ay-ts-deno

To test run ./ay.ts then pass in test.ay as a CLI argument (I'm build a JIT transpiler with deno)

from deno.

dsherret avatar dsherret commented on July 20, 2024

@MikeyA-yo it's because the array is empty. .every returns true for empty arrays, but some will return false.

from deno.

MikeyA-yo avatar MikeyA-yo commented on July 20, 2024

@MikeyA-yo it's because the array is empty. .every returns true for empty arrays, but some will return false.

No, not on my end, when i log the array, i see values i need, also it works on bun and node, exactly similar code, so that can't be it, but i'll leave it as that since you already added invalid tag.

Important Note The Array Is Not Empty

from deno.

MikeyA-yo avatar MikeyA-yo commented on July 20, 2024

Ran a script that tried every version from 1.30.0 to 1.43.6. All of them work. Given that those Array methods are defined inside v8 itself, it seems unlikely that the bug is there. Otherwise the same issue would be present in Node or Chrome. Maybe something else is going on in the code and it fails elsewhere?

So i did some login again and this is exactly what i have as the array values of the 2 elements:

importsForV: [ " no", " tests" ]
exports:  [ "no", "tests" 
]```

it still doesn't work and i just upgraded my version

@dsherret I did some console.log and that is exactly what i see, so the array isn't empty

from deno.

MikeyA-yo avatar MikeyA-yo commented on July 20, 2024

@dsherret i'll leave this closed but i'd appreciate if you can try to look more into it

from deno.

0f-0b avatar 0f-0b commented on July 20, 2024

Do the strings contain whitespace? Strings with different amounts or different kinds of whitespace compare unequal. Only strings with exactly the same characters at the same positions are equal. For example, " no" === "no" returns false because the left-hand string starts with a space character while the right-hand string does not.

from deno.

dsherret avatar dsherret commented on July 20, 2024

@MikeyA-yo when I run:

./ay.ts test.ay

...in that repo, I get an empty array for exporters.

from deno.

MikeyA-yo avatar MikeyA-yo commented on July 20, 2024

Do the strings contain whitespace? Strings with different amounts or different kinds of whitespace compare unequal. Only strings with exactly the same characters at the same positions are equal. For example, " no" === "no" returns false because the left-hand string starts with a space character while the right-hand string does not.

Yes it did

from deno.

MikeyA-yo avatar MikeyA-yo commented on July 20, 2024

Where are you logging it, how can we have different results, now i don't understand the reason for this

from deno.

dsherret avatar dsherret commented on July 20, 2024

image

from deno.

MikeyA-yo avatar MikeyA-yo commented on July 20, 2024

image

wow, ohk

from deno.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.