Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Cat - A Statically Typed Programming Language Interpreter in C#

, 4 Nov 2006
This article contains the public domain implementation of an interpreter for a statically typed stack-based programming language in C# called Cat. The accompanying article is a high-level description of how the various modules work, a brief description of the language, and links to related work.
/// Public domain code by Christopher Diggins
/// http://www.cat-language.com

using System;
using System.Collections.Generic;
using System.Text;

namespace Cat
{
    /// <summary>
    /// This is the base class for simple types, function types, type variables, 
    /// type variable declarations, and type stacks.
    /// </summary>
    public abstract class CatType : CatBase
    {
        #region private static fields
        /// <summary>
        /// This object is used for parsing strings to create trees.
        /// </summary>
        private static Parser gpTypeParser = new Parser();
        #endregion

        /// <summary>
        /// These static fields are used to share common type definitions. 
        /// This is an efficiency issue, why make multiple copies, when 
        /// sharing one will do fine.
        /// </summary>
        #region public static fields
        public static CatType IntType = MakeCatType("int");
        public static CatType StringType = MakeCatType("string");
        public static CatType CharType = MakeCatType("char");
        public static CatType BoolType = MakeCatType("bool");
        public static CatType ListType = MakeCatType("lookup");
        public static CatType TypeType = MakeCatType("typeid");
        public static CatType VarType = MakeCatType("var");
        #endregion

        #region static public functions
        static public CatType MakeCatType(string sType)
        {
            AstNode pNode = gpTypeParser.ParseType(sType);
            if (pNode == null) Parser.Error(pNode, "illegal type expression " + sType);
            CatType t = MakeCatType(pNode);
            if (t is FxnType)
                (t as FxnType).Initialize();
            return t;
        }
        static public CatType MakeCatType(AstNode pNode)
        {
            if (pNode.node_type == NodeType.SimpleType)
            {
                return new SimpleType(pNode);
            }
            else if (pNode.node_type == NodeType.FxnType)
            {
                return new FxnType(pNode);
            }
            else if (pNode.node_type == NodeType.VarDecl)
            {
                return new TypeVarDecl(pNode);
            }
            else
            {
                Parser.Error(pNode, "invalid type declaration");
                return null; // hides warnings, but is unreachable code
            }
        }
        public bool IsAnyType()
        {
            return (this is SimpleType) && (ToString() == "any");
        }
        public bool IsMultiVar()
        {
            return IsTypeVar() && !AsDecl().IsSingle();
        }
        public bool IsSingleVar()
        {
            return IsTypeVar() && AsDecl().IsSingle();
        }
        public bool IsTypeVar()
        {
            return (this is TypeVar) || (this is TypeVarDecl);
        }
        public TypeVarDecl AsDecl()
        {
            if (this is TypeVarDecl) return this as TypeVarDecl;
            if (this is TypeVar) return (this as TypeVar).GetDecl();
            Assert(false, "this object is not a type variable or type variable declaration");
            return null;
        }
        #endregion 

        #region abstract functions
        public abstract bool IsTypeEq(CatType t);
        public abstract CatType Clone();
        #endregion
    }

    /// <summary>
    /// This is the type of single values, and type variables 
    /// </summary>
    public class SimpleType : CatType
    {
        #region fields
        string msType;
        #endregion fields

        #region overrides
        public override string ToString()
        {
            return msType;
        }
        public override bool IsTypeEq(CatType t)
        {
            if (t == this) return true;
            if (t.IsAnyType()) return true;
            if (!(t is SimpleType)) return false;
            return t.ToString() == ToString();
        }
        public override CatType Clone()
        {
            return this;
        }
        #endregion

        #region constructors
        public SimpleType(AstNode pNode)
        {
            if ((pNode == null) || (pNode.node_type != NodeType.SimpleType) || (pNode.children.Count != 1))
            {
                Parser.Error(pNode, "invalid type declaration, expected a simple type");
            }
            msType = pNode.children[0].ToString();
        }
        public SimpleType(SimpleType x)
        {
            msType = x.msType;
        }
        public SimpleType(string s)
        {
            msType = s;
        }
        #endregion            
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Christopher Diggins
Software Developer Autodesk
Canada Canada
This article was written by Christopher Diggins, a computer science nerd who currently works at Autodesk as an SDK specialist.
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 4 Nov 2006
Article Copyright 2006 by Christopher Diggins
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid