A question about compiler design

  • 4 Replies
  • 308 Views
*

FlatAssembler

  • 762
  • Not a FE-er
A question about compiler design
« on: December 16, 2024, 06:52:30 AM »
When making a compiler, how do you implement an operator that potentially returns a string not present anywhere in the program?

I've tried to implement the TypeOf operator in AEC, which works similar to the typeof in JavaScript, except that, when invoked on a structure, it returns the typename of that structure (rather than "object"). For the most part, it works, however, there is a problem: the compiler crashes if one tries to access TypeOf(AddressOf(someStructure)) and the string "SomeStructurePointer" isn't present anywhere in the program. You can read more about it here:
https://github.com/FlatAssembler/AECforWebAssembly/issues/22

Here is how the strings are implemented in my programming language. Right after parsing, this function is invoked:
Code: [Select]
protected:
  std::set<std::string> getStringsInSubnodes() const {
    auto setToBeReturned = std::set<std::string>();
    if (text == "asm(" or text == "asm_i32(" or text == "asm_i64(" or
        text == "asm_f32(" or
        text == "asm_f64(") // Inline assembly isn't a string that can be
                            // manipulated, and storing it in memory wastes
                            // memory (potentially a lot of it).
      return std::set<std::string>(); // That's why we will return an empty set,
                                      // as if we had no strings in our subnodes
                                      // (even though we have at least one).
    if (text.size() and text[0] == '"') {
      setToBeReturned.insert(text);
      return setToBeReturned;
    }
    for (auto child : children) {
      auto stringsInChild = child.getStringsInSubnodes();
      setToBeReturned.insert(stringsInChild.begin(), stringsInChild.end());
    }
    // Now follows the code for the `TypeOf` operator. You can read more about
    // it here: https://langdev.stackexchange.com/q/4189/330
    for (auto basicDataType : basicDataTypeSizes)
      setToBeReturned.insert("\"" + basicDataType.first + "\"");
    if (isPointerType(text))
      setToBeReturned.insert("\"" + demanglePointerType(text) + "\"");
    if (text == "Structure")
      setToBeReturned.insert("\"" + children.at(0).text + "\"");
    return setToBeReturned;
  }
And then all the strings collected from the Abstract Syntax Tree into the set are put at the beginning of the heap memory, like this:
Code: [Select]
    auto allTheStrings = getStringsInSubnodes();
    for (auto string : allTheStrings) {
      context.globalVariables[string] = context.globalVariablePointer;
      context.variableTypes[string] = "CharacterPointer";
      if (string.back() != '"')
        string += '"';
      globalDeclarations += "\t(data 0 (i32.const " +
                            std::to_string(context.globalVariablePointer) +
                            ") " + string + ")\n";
      context.globalVariablePointer += string.size() - 1;
    }
And the very TypeOf operator is implemented like this:
Code: [Select]
  if (text == "TypeOf(") {
    if (children.size() != 1) {
      std::cerr << "Line " << lineNumber << ", Column " << columnNumber
                << ", Compiler error: The TypeOf operator has either no "
                   "children or has more than 1 child!"
                << std::endl;
      std::exit(1);
    }
    TreeNode newTreeNode =
        TreeNode("\"" + children.at(0).getType(context) + "\"", lineNumber,
                 columnNumber);
    return newTreeNode.compile(context);
  }
« Last Edit: December 16, 2024, 12:06:14 PM by FlatAssembler »
Fan of Stephen Wolfram.
This is my parody of the conspiracy theorists:
https://www.theflatearthsociety.org/forum/index.php?topic=71184.0
This is my attempt to refute the Flat-Earth theory:

*

Username

  • Administrator
  • 17990
Re: A question about compiler design
« Reply #1 on: December 16, 2024, 01:47:17 PM »
I may be misunderstanding the problem as I just quickly glanced it over, but why not just add it to the heap once you encounter it if its not present?
So long and thanks for all the fish

*

FlatAssembler

  • 762
  • Not a FE-er
Re: A question about compiler design
« Reply #2 on: December 17, 2024, 10:57:11 AM »
I may be misunderstanding the problem as I just quickly glanced it over, but why not just add it to the heap once you encounter it if its not present?
I don't understand what you mean by that.
Fan of Stephen Wolfram.
This is my parody of the conspiracy theorists:
https://www.theflatearthsociety.org/forum/index.php?topic=71184.0
This is my attempt to refute the Flat-Earth theory:

*

Username

  • Administrator
  • 17990
Re: A question about compiler design
« Reply #3 on: December 17, 2024, 11:07:17 AM »
I take it then I am misunderstanding it :). I'll try to spend some time looking at it later to give you a proper answer.
So long and thanks for all the fish

*

Username

  • Administrator
  • 17990
Re: A question about compiler design
« Reply #4 on: December 19, 2024, 12:46:18 AM »
Looks like I won't get the change. Cya.
So long and thanks for all the fish