mjarkk / general_programming_language

A temporarly repo for an idea to create a programming language that can be compiled into other languages
MIT License
1 stars 0 forks source link

[BUG] Function call fails to pass first character #5

Closed TheOtterlord closed 4 years ago

TheOtterlord commented 4 years ago

Thought it would be better to open an issue for this than use the OSI issue.

I noticed the Fixed the test_function_call test commit and equalised my fork. After rerunning the tests, I edited example.gpl to include a function call. Now for some reason it would not accept any newlines inside the function. But the real issue is that after checking the console log, I noticed that the parsed mpty as ActionFunctionCall.name when the function I was calling was named empty.

This was the console log outputted by main:

[Function { name: Some("empty"), args: [], body: Actions { list: [] } }, Function { name: Some("take_args"), args: [("a", Type { name: "int" }), ("b", Type { name: "int" })], body: Actions { list: [] } }, Function { name: Some("main"), args: [], body: Actions { list: [FunctionCall(ActionFunctionCall { name: "mpty", arguments: [] })] } }]

I also made the test log the output for further info, and got this:

running 1 test
Parser { index: 72, contents: [10, 32, 32, 32, 32, 32, 32, 102, 110, 32, 116, 101, 115, 116, 40, 41, 32, 123, 10, 32, 32, 32, 32, 32, 32, 125, 10, 32, 32, 32, 32, 32, 32, 102, 110, 32, 109, 97, 105, 110, 40, 41, 32, 123, 10, 32, 32, 32, 32, 32, 32, 32, 32, 116, 101, 115, 116, 40, 41, 10, 32, 32, 32, 32, 32, 32, 125, 10, 32, 32, 
32, 32], functions: [Function { name: Some("test"), args: [], body: Actions { list: [] } }, Function { name: Some("main"), args: [], body: Actions { list: [FunctionCall(ActionFunctionCall { name: "est", arguments: [] })] } }] }

My modified test looks like this:

#[test]
fn test_function_call_without_args() {
  let res = parse_str(
    r#"
      fn test() {
      }
      fn main() {
        test()
      }
    "#,
  );
  println!("{:?}", res);
}

My modified example.gpl

fn empty() {}

fn take_args(a int, b int) {}

fn main() { empty() }
TheOtterlord commented 4 years ago

I've narrowed down the problem to line 237 in src/lib/action.rs. Instead of calling next_char, we need to fetch the current character. As I do not know how the interactions work with variables, I thought I'd get your opinion before deciding to implement anything.

mjarkk commented 4 years ago

So i've fixed a few things in the action.rs file in this commit: https://github.com/mjarkk/general_programming_language/commit/cd4cb30fd90ced93e7410409fbaf11d146ff218f

Instead of calling next_char, we need to fetch the current character.

I think this is an issue with the try_match function that is used a bit earlier, that function seeks the next chars and resets the index to the index when the function started if it doesn't find anything.
Currently the counts all the cars and subtracts that of the index here:

    // Reset the index if we havent found the requested item
    self.index -= char_count;
    None
}

Maybe it's better if we make a copy of the original index at the top of this function and restore it at the end, this also reduces the cpu cycles needed to run this function.