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

question #1

Open
geraldstanje opened this issue Nov 16, 2014 · 14 comments
Open

question #1

geraldstanje opened this issue Nov 16, 2014 · 14 comments

Comments

@geraldstanje
Copy link

Hi,

did you also think about using: https://github.com/cznic/goyacc ?

Thanks,
-Gerald

@zellyn
Copy link
Owner

zellyn commented Nov 17, 2014

Nope: I didn't know about it. Looks like a willing-to-modify-yacc approach to http://people.via.ecp.fr/~stilgar/doc/compilo/parser/Generating%20LR%20Syntax%20Error%20Messages.pdf (as opposed to the approach taken by rsc (referencing the same paper) at http://research.swtch.com/yyerror.

Thanks for the pointer. I'll probably stick with the go tool yacc for this, since it's just a classroom compiler, and everyone is guaranteed to have it :-)

@geraldstanje
Copy link
Author

can you generate good error messages if the syntax of the cool source is not correct?

@zellyn
Copy link
Owner

zellyn commented Nov 18, 2014

For a classroom compiler, yes :-)

In fact, the test data they give you to check against has fairly typical
yacc errors in it, so it's actually easier to validate that I'm getting it
right if I have the same kind of (relatively uninformative) errors. :-/

Zellyn

On Tue Nov 18 2014 at 12:42:32 AM geraldstanje [email protected]
wrote:

can you generate good error messages if the syntax of the source is not
correct?


Reply to this email directly or view it on GitHub
#1 (comment).

@geraldstanje
Copy link
Author

any updates on error handling?
how would you implement preprocessor commands like #ifdef or #include ?

@zellyn
Copy link
Owner

zellyn commented Dec 5, 2014

I got a lot of mileage out of reading https://code.google.com/p/rsc/source/browse/cc/ - it does #include at the lexer level. I'm not sure about #ifdef.

@geraldstanje
Copy link
Author

where do you store variable declarations within functions/methods?

@zellyn
Copy link
Owner

zellyn commented Dec 11, 2014

Not quite sure I understand. Variables in Cool are either object attributes (

type Attr struct {
), function parameters (
Formals []*Formal
), or created with Let statements (
Let // Let expression
)

@geraldstanje
Copy link
Author

Sorry for the unclear question. I was saying you need to implement a stack for local variable declarations within functions...
so you traverse the AST and push the variable on the stack if there is a new VariableDecl. The variable will be poped from the stack if you leave the scope...

@zellyn
Copy link
Owner

zellyn commented Dec 12, 2014

Yep. But I'm just now implementing the compiler, so I have't got there yet. For typechecking, I'm using this: https://github.com/zellyn/gocool/blob/master/symbols/symbols.go

@geraldstanje
Copy link
Author

can you give me some feedback to my toy compiler? https://github.com/geraldstanje/toycompiler

i would also like to add function, if statements and variables ... im not sure how to implement scoping...any comments?

@zellyn
Copy link
Owner

zellyn commented Dec 19, 2014

Which areas in particular would you like feedback on?

I haven't got all the way through the implementation of scoping, but what's done is mostly in https://github.com/zellyn/gocool/blob/master/symbols/symbols.go

Basically, each time you encounter a variable in a new scope, you push it on the stack. Some implementations push a whole new scope on a stack of scopes. But Cool is so simple that I don't think I'm going to need those. You'll notice that queries of symbol tables run from the end backwards, so you always search the most nested scopes first.

I've been using the coursera compilers class as my reference: I think I've watched all the relevant videos at least twice now.

@geraldstanje
Copy link
Author

about the design of the toy compiler...i still need to add proper testing and split into packages...

so i would best add the scoping into https://github.com/geraldstanje/toycompiler/blob/master/codegen.go and add FuncCallNode, IfNode, VarDeclNode into func (c *AsmCodeGenerator) compNode(node Node)?

i guess i also need the prev scope in case i do:
void foo(int a) {
print a;
}

int main() {
int a = 2;
foo(a);
}

@zellyn
Copy link
Owner

zellyn commented Dec 19, 2014

So, at least for Cool, a function has its own scope (well, plus the Object properties). So foo wouldn't care about the a in main(). That would be dynamic scope, like in elisp. :-)

@geraldstanje
Copy link
Author

ok, i don't refer to cool :)

so for the variable a in foo I would need to lookup the varDecl in the prev scope?

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

No branches or pull requests

2 participants