Skip to content

Commit 20d9611

Browse files
Raymond LeiRaymond Lei
Raymond Lei
authored and
Raymond Lei
committed
rename
1 parent 1c23dff commit 20d9611

10 files changed

+352
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Text.RegularExpressions;
5+
6+
namespace CSharpClassLibrary.RandomComplier
7+
{
8+
public static class AlphabetHelper
9+
{
10+
public static Regex RegexLetter { get; } = new Regex("^[a-zA-Z]$");
11+
public static Regex RegexNumber { get; } = new Regex("^\\d$");
12+
public static Regex RegexLiteral { get; } = new Regex("^\\w$");
13+
public static Regex RegexOperator { get; } = new Regex("^[+-\\\\*/<>=!&|^%]$");
14+
15+
public static bool IsLetter(char c) => RegexLetter.IsMatch(c + "");
16+
public static bool IsNumber(char c) => RegexNumber.IsMatch(c + "");
17+
public static bool IsLiteral(char c) => RegexLiteral.IsMatch(c + "");
18+
public static bool IsOperator(char c) => RegexOperator.IsMatch(c + "");
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CSharpClassLibrary.RandomComplier
6+
{
7+
public static class KeyWords
8+
{
9+
public static string[] Keywords { get; } = {
10+
"if",
11+
"else",
12+
"for",
13+
"while",
14+
"break",
15+
"func",
16+
"return",
17+
"int",
18+
"float",
19+
"string",
20+
"boolean",
21+
"void"
22+
};
23+
24+
public static HashSet<string> Set { get; } = new HashSet<string>(Keywords);
25+
public static bool IsKeyword(string word) => Set.Contains(word);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.Serialization;
4+
using System.Text;
5+
6+
namespace CSharpClassLibrary.RandomComplier
7+
{
8+
[Serializable]
9+
public class LexicalException : Exception
10+
{
11+
public LexicalException() : base("Unexpected character") {}
12+
public LexicalException(string message) : base(string.Format("Unexpected character {0}", message)) {}
13+
14+
public LexicalException(string message, Exception innerException) : base(string.Format("Unexpected character {0}", message), innerException) {}
15+
16+
protected LexicalException(SerializationInfo info, StreamingContext context) : base(info, context) {}
17+
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace CSharpClassLibrary.RandomComplier
2+
{
3+
public class MakeStringContent
4+
{
5+
public IMakeStringState State { get; set; }
6+
public char C { get; set; }
7+
8+
public MakeStringContent(IMakeStringState startState) {
9+
State = startState;
10+
}
11+
12+
public string Concat(string s) => $"{s}{C}";
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace CSharpClassLibrary.RandomComplier
2+
{
3+
public class DoubleQuotesMakeStringState : IMakeStringState
4+
{
5+
public Token DoAction(MakeStringContent content, string s)
6+
{
7+
if (content.C == '"') {
8+
return new Token(TokenType.STRING, s);
9+
} else {
10+
return null;
11+
}
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CSharpClassLibrary.RandomComplier
6+
{
7+
public interface IMakeStringState
8+
{
9+
Token DoAction(MakeStringContent content, string s);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CSharpClassLibrary.RandomComplier
6+
{
7+
public class SingleQuotesMakeStringState : IMakeStringState
8+
{
9+
public Token DoAction(MakeStringContent content, string s)
10+
{
11+
if (content.C == '\'') {
12+
return new Token(TokenType.STRING, s);
13+
} else {
14+
return null;
15+
}
16+
}
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace CSharpClassLibrary.RandomComplier
2+
{
3+
public class StartMakeStringState : IMakeStringState
4+
{
5+
public Token DoAction(MakeStringContent content, string s) {
6+
if (content.C == '"') {
7+
content.State = new DoubleQuotesMakeStringState();
8+
} else {
9+
content.State = new SingleQuotesMakeStringState();
10+
}
11+
return null;
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using CSharpClassLibrary.IIterator;
2+
using System;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
7+
//https://stackoverflow.com/questions/1273001/is-there-a-hasnext-method-for-an-ienumerator
8+
9+
namespace CSharpClassLibrary.RandomComplier
10+
{
11+
public sealed class PeekableEnumerableAdapter<T> : IIterable<T>, IIterator<T>
12+
{
13+
public bool FetchedNext { get; private set; }
14+
public bool NextAvailable { get; private set; }
15+
public IEnumerable<T> Enumerable { get; }
16+
public IEnumerator<T> Enumerator { get; }
17+
public int CacheSize { get; } = 10;
18+
public Queue<T> Queue { get; } = new Queue<T>();
19+
public Stack<T> Stack { get; } = new Stack<T>();
20+
public T EndToken { get; private set; }
21+
public T NextObject { get; private set; }
22+
23+
public PeekableEnumerableAdapter(IEnumerable<T> enumerable)
24+
{
25+
Enumerable = enumerable;
26+
Enumerator = enumerable.GetEnumerator();
27+
}
28+
29+
public PeekableEnumerableAdapter(IEnumerable<T> enumerable, T endToken)
30+
{
31+
Enumerable = enumerable;
32+
Enumerator = enumerable.GetEnumerator();
33+
EndToken = endToken;
34+
}
35+
36+
public PeekableEnumerableAdapter(IEnumerator<T> enumerator)
37+
{
38+
Enumerator = enumerator;
39+
}
40+
41+
public IIterator<T> Iterator()
42+
{
43+
return new PeekableEnumerableAdapter<T>(Enumerable.GetEnumerator());
44+
}
45+
46+
public bool HasNext
47+
{
48+
get
49+
{
50+
if (EndToken != null || Stack.Count > 0) return true;
51+
CheckNext();
52+
return NextAvailable;
53+
}
54+
}
55+
56+
57+
public T Peek()
58+
{
59+
if (Stack.Count > 0)
60+
{
61+
return Stack.Peek();
62+
}
63+
if (!HasNext)
64+
{
65+
return EndToken;
66+
}
67+
T val = Next();
68+
PutItBack();
69+
return val;
70+
}
71+
72+
public T Next()
73+
{
74+
if (Stack.Count > 0)
75+
{
76+
T NextObject = Stack.Pop();
77+
while (Queue.Count > CacheSize - 1)
78+
{
79+
Queue.Dequeue();
80+
}
81+
Queue.Enqueue(NextObject);
82+
return NextObject;
83+
}
84+
else
85+
{
86+
CheckNext();
87+
if (!NextAvailable)
88+
{
89+
T tmp = EndToken;
90+
EndToken = default;
91+
return tmp;
92+
}
93+
while (Queue.Count > CacheSize - 1)
94+
{
95+
Queue.Dequeue();
96+
}
97+
Queue.Enqueue(NextObject);
98+
FetchedNext = false;
99+
return NextObject;
100+
}
101+
}
102+
103+
private void CheckNext()
104+
{
105+
if (!FetchedNext)
106+
{
107+
NextAvailable = Enumerator.MoveNext();
108+
if (NextAvailable)
109+
{
110+
NextObject = Enumerator.Current;
111+
}
112+
FetchedNext = true;
113+
}
114+
}
115+
116+
public void Remove()
117+
{
118+
throw new NotSupportedException();
119+
}
120+
121+
public void PutItBack()
122+
{
123+
if (Queue.Count > 0)
124+
{
125+
Queue.Reverse();
126+
Stack.Push(Queue.Dequeue());
127+
Queue.Reverse();
128+
}
129+
}
130+
131+
public void Dispose()
132+
{
133+
Enumerator.Dispose();
134+
}
135+
}
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
3+
namespace CSharpClassLibrary.RandomComplier
4+
{
5+
public enum TokenType
6+
{
7+
KEYWORD,
8+
VARIABLE,
9+
OPERATOR,
10+
BRACKET,
11+
INTEGER,
12+
STRING,
13+
FLOAT,
14+
BOOLEAN
15+
}
16+
17+
public class Token {
18+
public Token(TokenType type, string value)
19+
{
20+
Type = type;
21+
Value = value;
22+
}
23+
24+
public TokenType Type { get; set; }
25+
26+
public string Value { get; set; }
27+
new public string ToString => string.Format("type {0}, value {1}", Type, Value);
28+
29+
public bool IsScalar() {
30+
return Type == TokenType.INTEGER || Type == TokenType.FLOAT
31+
|| Type == TokenType.STRING || Type == TokenType.BOOLEAN;
32+
}
33+
34+
public bool IsVariable => Type == TokenType.VARIABLE;
35+
36+
public bool IsType() {
37+
return Value.Equals("bool") || Value.Equals("int")
38+
|| Value.Equals("float") || Value.Equals("void")
39+
|| Value.Equals("string");
40+
}
41+
42+
public static Token ParsingUsingIterator(PeekableEnumerableAdapter<char> iterator) {
43+
string s = "";
44+
while (iterator.HasNext) {
45+
char lookahead = iterator.Peek();
46+
if (AlphabetHelper.IsLetter(lookahead)) {
47+
s += lookahead;
48+
} else {
49+
break;
50+
}
51+
iterator.Next();
52+
}
53+
if (KeyWords.IsKeyword(s)) {
54+
return new Token(TokenType.KEYWORD, s);
55+
}
56+
if (s.Equals("true") || s.Equals("false")) {
57+
return new Token(TokenType.BOOLEAN, s);
58+
}
59+
return new Token(TokenType.VARIABLE,s);
60+
}
61+
62+
public static Token MakeString(PeekableEnumerableAdapter<char> iterator) {
63+
string s = "";
64+
IMakeStringState startState = new StartMakeStringState();
65+
MakeStringContent content = new MakeStringContent(startState);
66+
while (iterator.HasNext) {
67+
char c = iterator.Next();
68+
content.C = c;
69+
Token token = content.State.DoAction(content, s + c);
70+
if (token != null) {
71+
return token;
72+
} else {
73+
s = content.Concat(s);
74+
}
75+
}
76+
throw new LexicalException("Unexpected error");
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)