Skip to content

Instantly share code, notes, and snippets.

View gaufung's full-sized avatar
Focusing

Fung Kao gaufung

Focusing
View GitHub Profile
// ParserTest.cs
[TestFixture]
public class ParserTest
{
//...
[Test]
public void TestParser()
{
var tests = new []
{
@gaufung
gaufung / calculator_parser_2.cs
Created September 13, 2020 08:27
Calculaotr parser 2
public class Parser
{
//...
public Expression Parse()
{
return this.ParseExpression(Precedence.LOWEST);
}
private Expression ParseExpression(Precedence precedence)
{
if(this.PrefixFns.ContainsKey(this._currentToken.Type))
@gaufung
gaufung / calculator_parser.cs
Created September 13, 2020 08:25
Calculator Parser
//Pecedence.cs
public enum Precedence
{
LOWEST = 0,
SUM = 1,
PRODUCT = 2,
PREFIX = 3,
POWER = 4,
GROUP = 5,
}
@gaufung
gaufung / calculator_expression.cs
Created September 13, 2020 08:23
Calculator Expression
public abstract class Expression
{
public abstract string TokenLiteral();
}
//IntegerExpression.cs
public class IntegerExpression : Expression
{
public Token Token { get; set; }
public long Value { get; set; }
//...
@gaufung
gaufung / calculator_lext.cs
Created September 13, 2020 08:09
Calculator Lexer
// Lexer.cs
public class Lexer
{
//...
public Token NextToken()
{
//...
char character = this.input[this.position];
switch (character)
{
@gaufung
gaufung / lexer_test.cs
Created September 13, 2020 08:07
Lexer Test
[Test]
public void TestLexerNextToken()
{
string input = @"1 + 2 * (12 - 6) / 3 + 2 ^ 3
+ ((4+3) * 4)
/ 4
";
var tests = new[]
{
@gaufung
gaufung / calculator_token_class.cs
Created September 13, 2020 08:03
Calculaotr token class
//Token.cs
public class Token
{
public TokenType Type { get; set; }
public string Literal { get; set; }
}
@gaufung
gaufung / Calculator_token.cs
Created September 13, 2020 08:02
Calculator Token
//Token.cs
public enum TokenType
{
INT,
PLUS,
MINUS,
MULTIPLY,
DIVIDE,
POWER,
LPAREN,
@gaufung
gaufung / Calcluaotr_prototype.cs
Created September 13, 2020 07:51
Calculator prototype
// program.cs
static void Main(string[] args)
{
const string WELCOME = "Welcome to Calculator. Feel free to type any expression you want.";
const string PROMPT = ">> ";
Console.Out.Write(WELCOME + Environment.NewLine);
while(true)
{
Console.Out.Write(PROMPT);
try
@gaufung
gaufung / rabbitmq_asynchronous_receive.py
Created September 12, 2020 13:44
Rabbitmq asyncrhonous receive
@gen.coroutine
def receive(self, exchange, routing_key, queue_name, handler, no_ack=False, prefetch_count=0):
"""
receive message. creating a brand new channel to consume message. Before consuming, it have to declaring
exchange and queue. And bind queue to particular exchange with routing key. if received properties is not
none, it publishes result back to `reply_to` queue.
:param exchange: exchange name
:param routing_key: routing key (e.g. dog.*, *.big)
:param queue_name: queue name
:param handler: message handler,