Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement unary - #17

Merged

Conversation

yutannihilation
Copy link
Contributor

Currently, this code

fn dsp() {
    2.0-2.0
}

cannot be parsed.

Filename: \\?\C:\Users\Yutani\Documents\GitHub\mimium-rs\tmp.mmm
, ror: unexpected token something else, expected ^, ;,
, (, |, -, /, ", :, *, <, !, =, 0, ., +, }, [, ), %, ,, &, {, ], >
   ╭─[\\?\C:\Users\Yutani\Documents\GitHub\mimium-rs\tmp.mmm:2:3]
   │
 2 │     2.0-2.0
   ·   ┬
   ·   ╰── Unexpected token
───╯
Error: unexpected token while parsing apply, expected ||, linebreak, }, |>, >, -, <=, +, !=, /, ;, >=, &&, %, (, ==, <, *, ^
   ╭─[\\?\C:\Users\Yutani\Documents\GitHub\mimium-rs\tmp.mmm:2:8]
   │
 2 │     2.0-2.0
   ·        ──┬─
   ·          ╰─── Unexpected token -2.0
───╯

This is because there's no parser for unary operations. Currently, the lexer parses - as part of a float literal.

let float = just('-')
.or_not()
.then(
text::int(10)
.then(just('.'))
.then(text::digits(10).or_not()),
)

It might be a choice to require spaces before and after - to distinguish the binary op and the unary op. But, I think users expect this syntax is valid, so mimium anyway needs a unary parser.

-x

@tomoyanonymous
Copy link
Owner

Thanks! Maybe should we add more test cases with nested operation to ensure the parse works correctly?

    run_simple_test("- -1.0", 1.0, 3);
    run_simple_test("-(-hoge)", 2.0, 3);

@yutannihilation
Copy link
Contributor Author

Thanks. Sure, I'll add some more test cases.

@yutannihilation
Copy link
Contributor Author

It seems a function call like this is not handled properly, so my implementation is not correct. Let me think.

-cos(0.0)

@tomoyanonymous
Copy link
Owner

Now parser emits ast for -cos(0.0) to (app (app neg (cos))((float 0.0)))). ((- cos) (0.0))
(You can emit raw ast as lisp-like syntax with --emit-ast option )

Now precedence of unary operator is bigger than apply. It should be just in between apply and other binary operators.

@yutannihilation
Copy link
Contributor Author

Now precedence of unary operator is bigger than apply. It should be just in between apply and other binary operators.

You are right, thanks. Should be fixed now!

@tomoyanonymous tomoyanonymous merged commit 93d7360 into tomoyanonymous:dev Aug 8, 2024
1 check passed
@yutannihilation yutannihilation deleted the implement-unary-neg branch August 8, 2024 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants