コード例 #1
0
ファイル: ByteCodeEncoder.cs プロジェクト: geofrey/crayon
		// the encoded byte code begins with the word CRAYON followed by a @ char.
		// The word crayon serves as a version stamp. For example, in a later version of byte code, CRAYON can be
		// swapped out with CRAYON2.
		// The first @ will always be the terminator of the version ID.

		// Following the @ will be an encoded integer indicating how many rows are in the byte code.
		// After that is each encoded row.
		public static String Encode(ByteBuffer buffer)
		{
			StringBuilder sb = new StringBuilder();
			sb.Append("CRAYON@");

			int[][] code = buffer.ToIntList().ToArray();
			string[] stringArgs = buffer.ToStringList().ToArray();
			int size = code.Length;

			sb.Append(EncodeInteger(size));

			for (int i = 0; i < size; ++i)
			{
				int[] row = code[i];
				string stringArg = stringArgs[i];
				EncodeRow(sb, row, stringArg);
			}
			return sb.ToString();
		}
コード例 #2
0
ファイル: ByteCodeEncoder.cs プロジェクト: geofrey/crayon
        // the encoded byte code begins with the word CRAYON followed by a @ char.
        // The word crayon serves as a version stamp. For example, in a later version of byte code, CRAYON can be
        // swapped out with CRAYON2.
        // The first @ will always be the terminator of the version ID.

        // Following the @ will be an encoded integer indicating how many rows are in the byte code.
        // After that is each encoded row.
        public static String Encode(ByteBuffer buffer)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("CRAYON@");

            int[][]  code       = buffer.ToIntList().ToArray();
            string[] stringArgs = buffer.ToStringList().ToArray();
            int      size       = code.Length;

            sb.Append(EncodeInteger(size));

            for (int i = 0; i < size; ++i)
            {
                int[]  row       = code[i];
                string stringArg = stringArgs[i];
                EncodeRow(sb, row, stringArg);
            }
            return(sb.ToString());
        }
コード例 #3
0
ファイル: AbstractPlatform.cs プロジェクト: falun/crayon
        private string GenerateReadableByteCode(ByteBuffer byteCode)
        {
            List <string> output     = new List <string>();
            List <int[]>  rows       = byteCode.ToIntList();
            List <string> stringArgs = byteCode.ToStringList();
            int           length     = rows.Count;
            int           rowLength;

            int[] row;
            Dictionary <int, string> opCodes = new Dictionary <int, string>();

            foreach (OpCode opCode in Enum.GetValues(typeof(OpCode)))
            {
                opCodes[(int)opCode] = opCode.ToString();
            }
            List <string> rowOutput = new List <string>();

            for (int i = 0; i < length; ++i)
            {
                row       = rows[i];
                rowLength = row.Length;
                rowOutput.Add(i + "\t");
                rowOutput.Add(opCodes[row[0]] + ":");
                for (int j = 1; j < row.Length; ++j)
                {
                    rowOutput.Add(" " + row[j]);
                }

                string sArg = stringArgs[i];
                if (sArg != null)
                {
                    rowOutput.Add(" " + sArg.Replace("\n", "\\n").Replace("\r", "\\r").Replace("\t", "\\t"));
                }

                output.Add(string.Join("", rowOutput));
                rowOutput.Clear();
            }

            return(string.Join("\r\n", output));
        }
コード例 #4
0
ファイル: AbstractPlatform.cs プロジェクト: geofrey/crayon
		private void GenerateReadableByteCode(string path, ByteBuffer byteCode)
		{
			List<string> output = new List<string>();
			List<int[]> rows = byteCode.ToIntList();
			List<string> stringArgs = byteCode.ToStringList();
			int length = rows.Count;
			int rowLength;
			int[] row;
			Dictionary<int, string> opCodes = new Dictionary<int, string>();
			foreach (OpCode opCode in Enum.GetValues(typeof(OpCode)))
			{
				opCodes[(int)opCode] = opCode.ToString();
			}
			List<string> rowOutput = new List<string>();
			for (int i = 0; i < length; ++i)
			{
				row = rows[i];
				rowLength = row.Length;
				rowOutput.Add(i + "\t");
				rowOutput.Add(opCodes[row[0]] + ":");
				for (int j = 1; j < row.Length; ++j)
				{
					rowOutput.Add(" " + row[j]);
				}

				string sArg = stringArgs[i];
				if (sArg != null)
				{
					rowOutput.Add(" " + sArg.Replace("\n", "\\n").Replace("\r", "\\r").Replace("\t", "\\t"));
				}

				output.Add(string.Join("", rowOutput));
				rowOutput.Clear();
			}

			System.IO.File.WriteAllText(path, string.Join("\r\n", output));
		}
コード例 #5
0
ファイル: ByteCodeCompiler.cs プロジェクト: geofrey/crayon
		private void CompileLiteralStream(Parser parser, ByteBuffer buffer, IList<Expression> expressions, bool outputUsed)
		{
			if (expressions.Count == 1)
			{
				this.CompileExpression(parser, buffer, expressions[0], outputUsed);
			}
			else
			{
				ByteBuffer exprBuffer = new ByteBuffer();
				foreach (Expression expression in expressions)
				{
					this.CompileExpression(parser, exprBuffer, expression, outputUsed);
				}

				// Add the literal ID arg from the above literals to the arg list of a LITERAL_STREAM
				buffer.Add(
					expressions[0].FirstToken, 
					OpCode.LITERAL_STREAM, 
					exprBuffer.ToIntList().Reverse<int[]>().Select<int[], int>(args => args[1]).ToArray());
			}
		}
コード例 #6
0
ファイル: ByteCodeCompiler.cs プロジェクト: geofrey/crayon
		// Build the lookup table that maps PC's to tokens. There can be multiple tokens per PC, but it's up to the Op
		// to have a consistent convention to figure out the context of those tokens.
		private ByteBuffer BuildTokenData(ByteBuffer userCode)
		{
			Token[] tokens = userCode.ToTokenList().ToArray();
			int[][] rows = userCode.ToIntList().ToArray();

			int size = tokens.Length;
			ByteBuffer output = new ByteBuffer();
			// TODO: add command line flag for excluding token data. In that case, just return here.

			Token token;
			int[] row;
			for (int i = 0; i < size; ++i)
			{
				token = tokens[i];
				row = rows[i];

				if (token != null)
				{
					output.Add(null, OpCode.TOKEN_DATA, i, token.Line, token.Col, token.FileID);
				}
			}

			return output;
		}