Click here to Skip to main content
15,895,777 members
Articles / Programming Languages / Javascript

JSBasic - A BASIC to JavaScript Compiler

Rate me:
Please Sign up or sign in to vote.
4.93/5 (42 votes)
20 Jan 2013CPOL18 min read 166.9K   1.2K   85  
In this C# project, BASIC source code is compiled to JavaScript and run in a browser.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Irony.Compiler;
using JSBasic.Compiler.Nodes;
using System.IO;

namespace JSBasic.Compiler
{
	internal static class Utilities
	{

		/// <summary>
		/// An iterator to to a depth-first traversal of an Abstract-Syntax Tree.
		/// Very useful when combined with LINQ-to-Objects.
		/// </summary>
		public static IEnumerable<AstNode> DepthFirstTraversal(this AstNode startNode)
		{
			yield return startNode;
			GenericJsBasicNode childBearer = startNode as GenericJsBasicNode;
			if (childBearer != null)
			{
				foreach (AstNode child in childBearer.ChildNodes)
				{
					if (child is GenericJsBasicNode)
					{
						foreach (var item in DepthFirstTraversal(child))
						{
							yield return item;
						}
					}
					else
					{
						yield return child;
					}
				}
			}
		}

	}

	/// <summary>
	/// An error in some BASIC source code.
	/// </summary>
	public class BasicSyntaxErrorException : Exception
	{

		public BasicSyntaxErrorException() { }
		public BasicSyntaxErrorException(string message) : base(message) { }
		public BasicSyntaxErrorException(string message, Exception inner) : base(message, inner) { }
		protected BasicSyntaxErrorException(
		  System.Runtime.Serialization.SerializationInfo info,
		  System.Runtime.Serialization.StreamingContext context)
			: base(info, context) { }
	}

}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
China China
Daniel has a Bachelor of Science with First Class Honours from the University of Auckland, and has designed and developed software in companies large and small.

Comments and Discussions