git.haldean.org tau / 9d0ee1a
list view for displaying registers haldean 2 years ago
6 changed file(s) with 102 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
55 xmlns:local="clr-namespace:Tau"
66 mc:Ignorable="d"
77 FocusManager.FocusedElement="{Binding ElementName=Input}"
8 Title="tau" Height="249.621" Width="159.735" WindowStyle="ToolWindow">
8 Title="tau" Height="338.621" Width="224.735" WindowStyle="ToolWindow">
99 <Grid>
1010 <Grid.RowDefinitions>
1111 <RowDefinition Height="*"/>
1212 <RowDefinition Height="Auto"/>
1313 <RowDefinition Height="40"/>
1414 </Grid.RowDefinitions>
15 <TextBlock Name="Result" TextWrapping="Wrap" Text="0" Margin="10,10,10,72" FontFamily="PragmataPro" FontSize="20"/>
15 <ListBox Name="Output" Margin="10" Grid.Row="0" BorderBrush="{x:Null}">
16 <ListBox.ItemTemplate>
17 <DataTemplate>
18 <StackPanel Orientation="Horizontal">
19 <TextBlock Margin="0,0,10,0" FontFamily="PragmataPro" FontSize="20" Text="{Binding Name, Mode=OneWay}"/>
20 <TextBlock FontFamily="PragmataPro" FontSize="20" Text="{Binding Value, Mode=OneWay}"/>
21 </StackPanel>
22 </DataTemplate>
23 </ListBox.ItemTemplate>
24 </ListBox>
1625 <TextBlock Name="Help" FontFamily="PragmataPro" FontSize="9" VerticalAlignment="Bottom" Grid.Row="1" Margin="10,0" TextWrapping="Wrap"/>
17 <TextBox Name="Input" Margin="10,0,10,10" TextWrapping="Wrap" VerticalAlignment="Bottom" FontFamily="PragmataPro" FontSize="20" KeyUp="InputKey" Grid.Row="2"/>
26 <TextBox Name="Input" Margin="10,0,10,10" TextWrapping="Wrap" VerticalAlignment="Bottom" FontFamily="PragmataPro" FontSize="20" KeyUp="InputKey" Grid.Row="2" BorderBrush="{x:Null}"/>
1827 </Grid>
1928 </Window>
2121 {
2222 StackMachine sm;
2323 OpParser parser;
24 TextBlock result;
24 ListBox output;
2525 TextBlock help;
2626
2727 public MainWindow()
2929 InitializeComponent();
3030 sm = new StackMachine();
3131 parser = OpParser.New();
32 result = (TextBlock)FindName("Result");
32 output = (ListBox)FindName("Output");
3333 help = (TextBlock)FindName("Help");
3434 }
3535
4242 if (e.Key == Key.Enter)
4343 {
4444 sm.Run(parsed);
45 result.Text = sm.State();
45 output.ItemsSource = sm.Items();
4646 t.Text = "";
4747 return;
4848 }
3232 public static OpParser New()
3333 {
3434 OpParser parser = new OpParser();
35
36 /* arithmetic */
3537 parser.AddOp(new Op("+", "adds the top two values", BinOp((v1, v2) => { return v2 + v1; })));
3638 parser.AddOp(new Op("*", "multiplies the top two values", BinOp((v1, v2) => { return v2 * v1; })));
3739 parser.AddOp(new Op("-", "subtracts the top value from the value below it", BinOp((v1, v2) => { return v2 - v1; })));
3840 parser.AddOp(new Op("/", "divides the top value into the value below it", BinOp((v1, v2) => { return v2 / v1; })));
39 parser.AddOp(new Op("dup", "duplicates the top value on the stack", (Stack<Double> values) => {
41
42 /* stack */
43 parser.AddOp(new Op("dup", "duplicates the top value on the stack", (Stack<Double> values) =>
44 {
4045 if (values.Count == 0)
4146 return;
4247 double v = values.Pop();
4348 values.Push(v);
4449 values.Push(v);
4550 }));
46 parser.AddOp(new Op("swap", "swaps the top two values on the stack", (Stack<Double> values) => {
51 parser.AddOp(new Op("swap", "swaps the top two values on the stack", (Stack<Double> values) =>
52 {
4753 if (values.Count < 2)
4854 return;
4955 double v1 = values.Pop();
5157 values.Push(v1);
5258 values.Push(v2);
5359 }));
60
61 /* trig */
62 parser.AddOp(new Op("sin", "the sine of the top value, assumes value is in radians", UnOp((v) => { return Math.Sin(v); })));
63 parser.AddOp(new Op("cos", "the cosine of the top value, assumes value is in radians", UnOp((v) => { return Math.Cos(v); })));
64 parser.AddOp(new Op("tan", "the tangent of the top value, assumes value is in radians", UnOp((v) => { return Math.Tan(v); })));
65 parser.AddOp(new Op("pi", "pushes the value of π onto the stack", Const(Math.PI)));
66 parser.AddOp(new Op("tau", "pushes the value of τ onto the stack", Const(Math.PI * 2)));
67 parser.AddOp(new Op("rad", "converts the top value from degrees to radians", UnOp((v) => { return Math.PI * v / 180; })));
68 parser.AddOp(new Op("deg", "converts the top value from radians to degrees", UnOp((v) => { return 180 * v / Math.PI; })));
69
5470 return parser;
5571 }
5672
6985 double v1 = values.Pop();
7086 double v2 = values.Count == 0 ? v1 : values.Pop();
7187 values.Push(func(v1, v2));
88 };
89 }
90 private delegate double Unary(double v);
91 private static Op.Visitor UnOp(Unary func)
92 {
93 return (Stack<Double> values) =>
94 {
95 if (values.Count == 0)
96 return;
97 double v = values.Pop();
98 values.Push(func(v));
99 };
100 }
101 private static Op.Visitor Const(double v)
102 {
103 return (Stack<Double> values) =>
104 {
105 values.Push(v);
72106 };
73107 }
74108
55
66 namespace Tau
77 {
8 public class StackItem
9 {
10 internal StackItem(string name, double v)
11 {
12 Name = name;
13 Value = v;
14 }
15 public string Name { get; }
16 public double Value { get; }
17
18 internal static string NameIndex(int i)
19 {
20 switch (i)
21 {
22 case 0: return "X";
23 case 1: return "Y";
24 case 2: return "Z";
25 case 3: return "A";
26 case 4: return "B";
27 case 5: return "C";
28 }
29 var sb = new StringBuilder();
30 sb.Append("R");
31 sb.Append(i);
32 return sb.ToString();
33 }
34
35 public override string ToString()
36 {
37 var sb = new StringBuilder();
38 sb.Append(Name);
39 sb.Append(" = ");
40 sb.Append(Value);
41 return sb.ToString();
42 }
43 }
44
845 public class StackMachine
946 {
1047 private Stack<Double> values;
1350 {
1451 values = new Stack<double>();
1552 }
16
53
1754 public void Run(IEnumerable<Op> stream)
1855 {
1956 foreach (Op o in stream)
2259 }
2360 }
2461
25 public string State()
62 public IEnumerable<StackItem> Items()
2663 {
27 List<Double> st = values.ToList();
28 st.Reverse();
29 StringBuilder res = new StringBuilder();
30 foreach (double s in st)
31 {
32 res.Append(s);
33 res.Append("\n");
34 }
35 return res.ToString();
64 return from v in values.Zip(Enumerable.Range(0, values.Count),
65 (double v, int i) => { return new { Idx = i, V = v }; })
66 select new StackItem(StackItem.NameIndex(v.Idx), v.V);
3667 }
68
3769 }
3870 }
3535 <ItemGroup>
3636 <Reference Include="System" />
3737 <Reference Include="System.Data" />
38 <Reference Include="System.ValueTuple, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
39 <HintPath>..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
40 </Reference>
3841 <Reference Include="System.Xml" />
3942 <Reference Include="Microsoft.CSharp" />
4043 <Reference Include="System.Core" />
8689 <Generator>ResXFileCodeGenerator</Generator>
8790 <LastGenOutput>Resources.Designer.cs</LastGenOutput>
8891 </EmbeddedResource>
92 <None Include="packages.config" />
8993 <None Include="Properties\Settings.settings">
9094 <Generator>SettingsSingleFileGenerator</Generator>
9195 <LastGenOutput>Settings.Designer.cs</LastGenOutput>
0 <?xml version="1.0" encoding="utf-8"?>
1 <packages>
2 <package id="System.ValueTuple" version="4.3.0" targetFramework="net452" />
3 </packages>