Advertisement
Guest User

/g/ lang

a guest
Nov 1st, 2011
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. // lol C#    
  2.  
  3. public class GStackVar
  4.     {
  5.         public Int32 name;
  6.         public Double value;
  7.  
  8.         public GStackVar(String name, Double value)
  9.         {
  10.             this.name = name.GetHashCode();
  11.             this.value = value;
  12.         }
  13.     }
  14.  
  15.  
  16.         static GStackVar FindInStack(List<GStackVar> stack, String name)
  17.         {
  18.             Int32 n;
  19.  
  20.             n = name.GetHashCode();
  21.             foreach (GStackVar var in stack)
  22.             {
  23.                 if (var.name == n)
  24.                     return var;
  25.             }
  26.  
  27.             return null;
  28.         }
  29.  
  30.         // do it math? nope.
  31.         static Double Evaluate(String[] line, Int32 start)
  32.         {
  33.             Double value;
  34.  
  35.             if (!Double.TryParse(line[start], out value))
  36.                 throw new Exception();
  37.  
  38.             return value;
  39.         }
  40.  
  41.         static void Main()
  42.         {
  43.             StreamReader reader;
  44.             List<GStackVar> stack;
  45.             Int32 linenum;
  46.             String line;
  47.             String[] split;
  48.             GStackVar var;
  49.  
  50.             reader = new StreamReader("C:\\main.g");
  51.             stack = new List<GStackVar>();
  52.             linenum = 0;
  53.  
  54.             while (!reader.EndOfStream)
  55.             {
  56.                 line = reader.ReadLine();
  57.                 if (String.IsNullOrEmpty(line))
  58.                     continue;
  59.                 if (line[0] != '>')
  60.                     continue;
  61.  
  62.                 line = line.Substring(1);
  63.                 split = line.Split(' ');
  64.  
  65.                 if (split[0] == "implying" && split[2] == "=")
  66.                 {
  67.                     var = FindInStack(stack, split[1]);
  68.  
  69.                     if (var == null && !Char.IsNumber(split[1][0]))
  70.                         stack.Add(new GStackVar(split[1], Evaluate(split, 3)));
  71.                     else
  72.                         var.value = Evaluate(split, 3);
  73.                 }
  74.                 else if (split[0] == "mfw")
  75.                 {
  76.                     var = FindInStack(stack, split[1]);
  77.  
  78.                     if (var == null)
  79.                         Console.WriteLine("line {0}: Unknown variable \"{1}\"", linenum, split[1]);
  80.                     else
  81.                         Console.WriteLine(var.value);
  82.                 }
  83.                 else if (split[0] == "yfw")
  84.                 {
  85.                     var = FindInStack(stack, split[1]);
  86.  
  87.                     if (var == null)
  88.                         Console.WriteLine("line {0}: Unknown variable \"{1}\"", linenum, split[1]);
  89.                     else
  90.                     {
  91.                         var.value = Double.Parse(Console.ReadLine());
  92.                     }
  93.                 }
  94.                 else
  95.                 {
  96.                     Console.WriteLine("line {0}: Unknown instruction \"{1}\"", linenum, split[0]);
  97.                 }
  98.  
  99.                 linenum++;
  100.             }
  101.         }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement