Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Create character literal token
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesbetros committed Oct 23, 2020
1 parent 8c69175 commit 937ceea
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 41 deletions.
26 changes: 13 additions & 13 deletions playground/IncludeTest.asm
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,55 @@ Mov [DebugStub_v1], DebugStub_Const_const

; AH = 14
Mov AH, 0xE
; AL = H
; AL = 'H'
Mov AL, 0x48
; //! int 0x10
int 0x10
; AL = e
; AL = 'e'
Mov AL, 0x65
; //! int 0x10
int 0x10
; AL = l
; AL = 'l'
Mov AL, 0x6C
; //! int 0x10
int 0x10
; AL = l
; AL = 'l'
Mov AL, 0x6C
; //! int 0x10
int 0x10
; AL = o
; AL = 'o'
Mov AL, 0x6F
; //! int 0x10
int 0x10
; AL = 32
; AL = ' '
Mov AL, 0x20
; //! int 0x10
int 0x10
; AL = f
; AL = 'f'
Mov AL, 0x66
; //! int 0x10
int 0x10
; AL = r
; AL = 'r'
Mov AL, 0x72
; //! int 0x10
int 0x10
; AL = o
; AL = 'o'
Mov AL, 0x6F
; //! int 0x10
int 0x10
; AL = m
; AL = 'm'
Mov AL, 0x6D
; //! int 0x10
int 0x10
; AL = 32
; AL = ' '
Mov AL, 0x20
; //! int 0x10
int 0x10
; AL = X
; AL = 'X'
Mov AL, 0x58
; //! int 0x10
int 0x10
; AL = 35
; AL = '#'
Mov AL, 0x23
; //! int 0x10
int 0x10
Expand Down
26 changes: 13 additions & 13 deletions playground/Test.asm
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,55 @@ Mov [DebugStub_v1], DebugStub_Const_const

; AH = 14
Mov AH, 0xE
; AL = H
; AL = 'H'
Mov AL, 0x48
; //! int 0x10
int 0x10
; AL = e
; AL = 'e'
Mov AL, 0x65
; //! int 0x10
int 0x10
; AL = l
; AL = 'l'
Mov AL, 0x6C
; //! int 0x10
int 0x10
; AL = l
; AL = 'l'
Mov AL, 0x6C
; //! int 0x10
int 0x10
; AL = o
; AL = 'o'
Mov AL, 0x6F
; //! int 0x10
int 0x10
; AL = 32
; AL = ' '
Mov AL, 0x20
; //! int 0x10
int 0x10
; AL = f
; AL = 'f'
Mov AL, 0x66
; //! int 0x10
int 0x10
; AL = r
; AL = 'r'
Mov AL, 0x72
; //! int 0x10
int 0x10
; AL = o
; AL = 'o'
Mov AL, 0x6F
; //! int 0x10
int 0x10
; AL = m
; AL = 'm'
Mov AL, 0x6D
; //! int 0x10
int 0x10
; AL = 32
; AL = ' '
Mov AL, 0x20
; //! int 0x10
int 0x10
; AL = X
; AL = 'X'
Mov AL, 0x58
; //! int 0x10
int 0x10
; AL = 35
; AL = '#'
Mov AL, 0x23
; //! int 0x10
int 0x10
26 changes: 13 additions & 13 deletions playground/Test.xs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@
.v1 = #const

AH = 14
AL = H
AL = 'H'
//! int 0x10
AL = e
AL = 'e'
//! int 0x10
AL = l
AL = 'l'
//! int 0x10
AL = l
AL = 'l'
//! int 0x10
AL = o
AL = 'o'
//! int 0x10
AL = 32
AL = ' '
//! int 0x10
AL = f
AL = 'f'
//! int 0x10
AL = r
AL = 'r'
//! int 0x10
AL = o
AL = 'o'
//! int 0x10
AL = m
AL = 'm'
//! int 0x10
AL = 32
AL = ' '
//! int 0x10
AL = X
AL = 'X'
//! int 0x10
AL = 35
AL = '#'
//! int 0x10
47 changes: 47 additions & 0 deletions source/Spruce/Tokens/CharacterLiteral.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;

namespace Spruce.Tokens
{
public class CharacterLiteral : Token
{
// We are overriding the parse method to set rStart itself
public override object Parse(string aText, ref int rStart)
{
int i;

var isEscaped = false;
var foundEndQuote = false;

for (i = 1; i < aText.Length - rStart; i++)
{
if (aText[rStart + i] == '\\')
{
isEscaped = true;
continue;
}

if (aText[rStart + i] == '\'' && !isEscaped)
{
foundEndQuote = true;
i += 1; // Increment i so that the end quote is also included
break;
}

isEscaped = false;
}

// Cannot find end quote. We cannot parse the object as string.
if (!foundEndQuote)
return null;

// Replace all \' with ' and then remove the opening quotes
var xResult = aText.Substring(rStart + 1, i - 2).Replace(@"\'", @"'");
if (xResult.Length != 1)
{
throw new ArgumentOutOfRangeException("CharacterLiteral should only be a single character.");
}
rStart += i;
return xResult;
}
}
}
4 changes: 2 additions & 2 deletions source/XSharp/XSharp/Emitters/x86/Assignments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ protected void RegAssignNum(Register aDestReg, string aEquals, object aVal)
Asm.Emit(OpCode.Mov, aDestReg, aVal);
}

[Emitter(typeof(Reg), typeof(OpEquals), typeof(Alpha))]
protected void RegAssignAlpha(Register aReg, string aEquals, string aVal)
[Emitter(typeof(Reg), typeof(OpEquals), typeof(CharacterLiteral))]
protected void RegAssignStringListeral(Register aReg, string aEquals, string aVal)
{
// This will only ever be a single character
byte[] chars = Encoding.ASCII.GetBytes(aVal);
Expand Down

0 comments on commit 937ceea

Please sign in to comment.