Skip to content

Commit

Permalink
Adds this to method parameter list for methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaiah Becker-Mayer committed Jun 17, 2024
1 parent b10425d commit 71bc9a2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Compiler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ Tests TODO:
- ~ConvertToBin~
- ~Square~
- ~Average~
- Pong
- ~Pong~
- ComplexArrays
21 changes: 10 additions & 11 deletions Compiler/pkg/compiler/compilationengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (ce *CompilationEngine) compileSubroutine() error {
return TraceError(err)
}

if err := ce.compileParameterList(); err != nil {
if err := ce.compileParameterList(subKind); err != nil {
return TraceError(err)
}

Expand Down Expand Up @@ -388,15 +388,6 @@ func (ce *CompilationEngine) getVarName() (string, error) {
return id, nil
}

// checkForIdentifier checks that the current token is an identifier
func (ce *CompilationEngine) checkForIdentifier() error {
// Next should be a varName
if ce.jt.TokenType() != identifier {
return TraceError(fmt.Errorf("expected an %v", identifier))
}
return nil
}

// checkForSymbol checks that the passed symbol is currently being parsed
func (ce *CompilationEngine) checkForSymbol(sym string) error {
if s, err := ce.jt.Symbol(); s != sym {
Expand Down Expand Up @@ -446,11 +437,19 @@ func (ce *CompilationEngine) eatSymbol(sym string) error {
}

// '(' ((type varName)(',' type varName)*)? ')'
func (ce *CompilationEngine) compileParameterList() error {
//
// subKind is ('constructor' | 'function' | 'method')
func (ce *CompilationEngine) compileParameterList(subKind string) error {
if err := ce.eatSymbol("("); err != nil {
return TraceError(err)
}

if subKind == "method" {
// The first argument of a method is always the object that the method is being called on.
// Therefore, we need to add one to the parameter list.
ce.st.Define("this", ce.className, KIND_ARG)
}

// While we have yet to hit the closing ")"
for sym, _ := ce.jt.Symbol(); sym != ")"; sym, _ = ce.jt.Symbol() {
// First token should be a type
Expand Down

0 comments on commit 71bc9a2

Please sign in to comment.