Exemplo n.º 1
0
 public override void FunctionDeclaration(Subroutine subroutine)
 {
     EmitStructDeclarationIfNotEmitted();
     Output.WriteLine("__WORD {0} () {{", FormatSubroutineName(subroutine.Name));
     EmitLocalsAndParameters(subroutine);
     ExtractParameters(subroutine);
 }
Exemplo n.º 2
0
 private void ExtractParameters(Subroutine subroutine)
 {
     foreach (Symbol param in subroutine.Parameters.Reverse())
     {
         Output.WriteLine("  {0} = __POP();", param.Name);
     }
 }
Exemplo n.º 3
0
 public override void MethodDeclaration(Subroutine subroutine)
 {
     EmitStructDeclarationIfNotEmitted();
     Output.WriteLine("__WORD {0} () {{", FormatSubroutineName(subroutine.Name));
     EmitLocalsAndParameters(subroutine);
     //By convention, 'this' is the first parameter of the subroutine
     //call, so it's the last thing on the stack.
     Output.WriteLine("  __WORD THIS;");
     Output.WriteLine("  THIS = __POP();");
     ExtractParameters(subroutine);
 }
Exemplo n.º 4
0
 private void EmitLocalsAndParameters(Subroutine subroutine)
 {
     foreach (Symbol localVar in subroutine.Locals)
     {
         Output.WriteLine("  __WORD {0};", localVar.Name);
     }
     foreach (Symbol param in subroutine.Parameters)
     {
         Output.WriteLine("  __WORD {0};", param.Name);
     }
 }
Exemplo n.º 5
0
 public override void ConstructorDeclaration(Subroutine subroutine)
 {
     EmitStructDeclarationIfNotEmitted();
     Output.WriteLine("__WORD {0} () {{", FormatSubroutineName(subroutine.Name));
     EmitLocalsAndParameters(subroutine);
     Output.WriteLine("  __WORD THIS;");
     //A constructor must allocate memory for its class and
     //assign the result to the THIS local variable.
     Output.WriteLine("  THIS = (__WORD) __ALLOC({0}*sizeof(__WORD));", GetClassSizeInWords());
     ExtractParameters(subroutine);
 }
Exemplo n.º 6
0
        private void ParseSubDecl()
        {
            Contract.Requires(IsNextTokenSubDecl());

            Token subKind    = NextToken();
            Token returnType = NextToken();
            Token subName    = NextToken();

            if (subKind.Type != TokenType.Keyword ||
                !new[] { "constructor", "method", "function" }.Contains(subKind.Value))
            {
                ThrowCompilationException("Expected: 'constructor', 'method', or 'function', but got: " + subKind.Value);
            }
            if (returnType.Type != TokenType.Keyword &&
                returnType.Type != TokenType.Ident)
            {
                ThrowCompilationException("Invalid return type '" + returnType.Value + "'");
            }
            if (subName.Type != TokenType.Ident)
            {
                ThrowCompilationException("Invalid subroutine name '" + subName.Value + "'");
            }

            _currentSub = new Subroutine(
                (SubroutineKind)Enum.Parse(typeof(SubroutineKind), subKind.Value, ignoreCase: true),
                _currentClassName, subName.Value, returnType.Value);

            if (_currentSub.Kind == SubroutineKind.Constructor &&
                _currentSub.ReturnType != _currentClassName)
            {
                ThrowCompilationException("Constructor must return '" + _currentClassName + "'");
            }

            Match(new Token(TokenType.Symbol, "("));
            ParseFormalParamList();
            Match(new Token(TokenType.Symbol, ")"));

            //NOTE: We notify the code generator of the new subroutine only
            //after we have its local variable declarations.

            ParseSubBody();

            _currentSub = null;
            _methodSymTable.Clear();
        }
Exemplo n.º 7
0
 public override void FunctionDeclaration(Subroutine subroutine)
 {
     Output.WriteLine(
         "FUNCTION {0}.{1}",
         subroutine.ClassName, subroutine.Name);
 }
Exemplo n.º 8
0
 public abstract void MethodDeclaration(Subroutine subroutine);
Exemplo n.º 9
0
 public abstract void ConstructorDeclaration(Subroutine subroutine);
Exemplo n.º 10
0
 public abstract void MethodDeclaration(Subroutine subroutine);
Exemplo n.º 11
0
 public abstract void FunctionDeclaration(Subroutine subroutine);
Exemplo n.º 12
0
 public override void FunctionDeclaration(Subroutine subroutine)
 {
     EmitStructDeclarationIfNotEmitted();
     Output.WriteLine("__WORD {0} () {{", FormatSubroutineName(subroutine.Name));
     EmitLocalsAndParameters(subroutine);
     ExtractParameters(subroutine);
 }
Exemplo n.º 13
0
 public abstract void FunctionDeclaration(Subroutine subroutine);
 public override void ConstructorDeclaration(Subroutine subroutine)
 {
     Output.WriteLine(
         "CONSTRUCTOR {0}.{1}",
         subroutine.ClassName, subroutine.Name);
 }
Exemplo n.º 15
0
 public override void MethodDeclaration(Subroutine subroutine)
 {
     Output.WriteLine(
         "METHOD {0}.{1}",
         subroutine.ClassName, subroutine.Name);
 }
 public override void MethodDeclaration(Subroutine subroutine)
 {
     Output.WriteLine(
         "METHOD {0}.{1}",
         subroutine.ClassName, subroutine.Name);
 }
 public override void FunctionDeclaration(Subroutine subroutine)
 {
     Output.WriteLine(
         "FUNCTION {0}.{1}",
         subroutine.ClassName, subroutine.Name);
 }
Exemplo n.º 18
0
 private void ExtractParameters(Subroutine subroutine)
 {
     foreach (Symbol param in subroutine.Parameters.Reverse())
     {
         Output.WriteLine("  {0} = __POP();", param.Name);
     }
 }
Exemplo n.º 19
0
 private void EmitLocalsAndParameters(Subroutine subroutine)
 {
     foreach (Symbol localVar in subroutine.Locals)
     {
         Output.WriteLine("  __WORD {0};", localVar.Name);
     }
     foreach (Symbol param in subroutine.Parameters)
     {
         Output.WriteLine("  __WORD {0};", param.Name);
     }
 }
Exemplo n.º 20
0
 public override void MethodDeclaration(Subroutine subroutine)
 {
     EmitStructDeclarationIfNotEmitted();
     Output.WriteLine("__WORD {0} () {{", FormatSubroutineName(subroutine.Name));
     EmitLocalsAndParameters(subroutine);
     //By convention, 'this' is the first parameter of the subroutine
     //call, so it's the last thing on the stack.
     Output.WriteLine("  __WORD THIS;");
     Output.WriteLine("  THIS = __POP();");
     ExtractParameters(subroutine);
 }
Exemplo n.º 21
0
        private void ParseSubDecl()
        {
            Contract.Requires(IsNextTokenSubDecl());

            Token subKind = NextToken();
            Token returnType = NextToken();
            Token subName = NextToken();

            if (subKind.Type != TokenType.Keyword ||
                !new[]{"constructor", "method", "function"}.Contains(subKind.Value))
            {
                ThrowCompilationException("Expected: 'constructor', 'method', or 'function', but got: " + subKind.Value);
            }
            if (returnType.Type != TokenType.Keyword &&
                returnType.Type != TokenType.Ident)
            {
                ThrowCompilationException("Invalid return type '" + returnType.Value + "'");
            }
            if (subName.Type != TokenType.Ident)
            {
                ThrowCompilationException("Invalid subroutine name '" + subName.Value + "'");
            }

            _currentSub = new Subroutine(
                (SubroutineKind) Enum.Parse(typeof (SubroutineKind), subKind.Value, ignoreCase: true),
                _currentClassName, subName.Value, returnType.Value);

            if (_currentSub.Kind == SubroutineKind.Constructor &&
                _currentSub.ReturnType != _currentClassName)
            {
                ThrowCompilationException("Constructor must return '" + _currentClassName + "'");
            }

            Match(new Token(TokenType.Symbol, "("));
            ParseFormalParamList();
            Match(new Token(TokenType.Symbol, ")"));

            //NOTE: We notify the code generator of the new subroutine only
            //after we have its local variable declarations.

            ParseSubBody();

            _currentSub = null;
            _methodSymTable.Clear();
        }
Exemplo n.º 22
0
 public override void ConstructorDeclaration(Subroutine subroutine)
 {
     EmitStructDeclarationIfNotEmitted();
     Output.WriteLine("__WORD {0} () {{", FormatSubroutineName(subroutine.Name));
     EmitLocalsAndParameters(subroutine);
     Output.WriteLine("  __WORD THIS;");
     //A constructor must allocate memory for its class and
     //assign the result to the THIS local variable.
     Output.WriteLine("  THIS = (__WORD) __ALLOC({0}*sizeof(__WORD));", GetClassSizeInWords());
     ExtractParameters(subroutine);
 }
Exemplo n.º 23
0
 public abstract void ConstructorDeclaration(Subroutine subroutine);
Exemplo n.º 24
0
 public override void ConstructorDeclaration(Subroutine subroutine)
 {
     Output.WriteLine(
         "CONSTRUCTOR {0}.{1}",
         subroutine.ClassName, subroutine.Name);
 }