What's new? | Help | Directory | Sign in
Google
cat-language
The Cat Programming Language Project
  
  
  
  
    
Search
for
Updated Apr 05, 2007 by cdiggins
Labels: Implementation
HowTheInterpreterWorks  
The Cat interpreter implementation in a nutshell.

Introduction

The Cat interpreter is written in C# and is completely public domain. One of the intentions of the Cat project is to help other people develop their own compilers and interpreters, by providing a simple open-source and non-toy implementation of a programming language interpreter to use and play with.

This document provides the so-called "ten thousand foot view" of how the interpreter works.

Cat Interpreter

This is pseudo-code that shows how the interpreter works:

Interpreter()
  RegisterPrimitivesInLookupTable()
  ParsingExpressionGrammar g = MakeGrammar()
  ParsingExpressionGrammarParser p = MakeParser(g)
  loop 
    string s = GetInput()
    try
      GenericAbstractSyntaxTree ast = p.Parse(s)
      CatAbstractSyntaxTree cat_ast = MakeCatAst(ast);           
      // only process the top-level nodes
      foreach TopLevelNode node in cat_ast do
        ProcessNode(node)
    except
      ReportError

ProcessNode(CatAstNode node)
  if (node is Definition)
    AddDefintion(node as Definition)
  else if (node is Expression)
    ProcessExpression(node is Expression)  

ProcessDefinition(CatDefinitionNode def)
  AddToFxnLookup(def.GetName(), def.GetExpressions())

ProcessExpression(CatExpressionNode expr)
  if (expr is Number)
    stack.Push(expr.GetNumber)
  else if (expr as String)
    stack.Push(expr.GetString)
  else if (expr as FunctionName)
    CatExpressionNode[] expr_list = FxnLookup(expr.GetFunctionName())
    foreach (node in expr_list)
      ProcessExpression(expr)
  else if (expr is QuotedFxn)
    stack.Push(expr.GetQuotedFxn)   

From Psuedo-Code to Real Code

The following list describes how the pseudo-code maps to an actual implementation:


Sign in to add a comment