amath  1.8.5
Simple command line calculator
program.cpp
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  */
29 
30 #include "amath.h"
31 #include "amathc.h"
32 #include "program.h"
33 #include "language.h"
34 #include "language_stdc.h"
35 #include "language_amiga.h"
36 #include "filesystem.h"
37 #include "filesystem_stdc.h"
38 #include "filesystem_amiga.h"
39 #include "preferences.h"
40 #include "preferences_stdc.h"
41 #include "preferences_amiga.h"
42 #include "lib/numb.h"
43 #include "lib/real.h"
44 #include "lib/ntextd.h"
45 #include "main/values.h"
46 #include "main/functionlist.h"
47 
49  Console(nullptr), shellMode(false), ansiMode(false), status(0)
50 {
53  ins = new RealNumber();
54 #if defined(AMIGA)
55  Language = new AmigaLanguage();
56  Filesystem = new AmigaFilesystem();
57  Preferences = new AmigaPreferences();
58 #else
59  Language = new StandardLanguage();
62 #endif
65 }
66 
68 {
69  delete Variables;
70  delete Functions;
71  delete Filesystem;
72  delete Output;
73  delete Input;
74  delete Language;
75  delete ins;
76 
78  delete Preferences;
79 }
80 
81 void Program::NewPositionalInput(short unsigned int base, short unsigned int digits)
82 {
83  delete Input;
84 
85  char fractionPoint = Language->GetFractionPoint();
86 
87  if (base == 10)
88  {
89  Input = new DecimalSystem(digits, fractionPoint);
90  }
91  else
92  {
93  Input = new PositionalNumeralSystem(base, digits, fractionPoint);
94  }
95 }
96 
97 void Program::NewPositionalOutput(short unsigned int base, short unsigned int digits)
98 {
99  delete Output;
100 
101  char fractionPoint = Language->GetFractionPoint();
102 
103  if (base == 10)
104  {
105  Output = new DecimalSystem(digits, fractionPoint);
106  }
107  else
108  {
109  Output = new PositionalNumeralSystem(base, digits, fractionPoint);
110  }
111 }
112 
113 void Program::SetPrompt(const char* text) const
114 {
115  if (Console != nullptr)
116  {
118  }
120 }
121 
123 {
124  return ins;
125 }
126 
128 {
129  delete ins;
130  ins = number->Clone();
131 }
132 
133 int Program::GetExitStatus() const
134 {
135  return status;
136 }
137 
139 {
141 }
142 
143 bool Program::GetAnsiMode() const
144 {
145  return ansiMode;
146 }
147 
148 void Program::SetAnsiMode(bool value)
149 {
150  if (Console != nullptr)
151  {
152  bool success = Console->SetAnsiMode(value);
153  if (!success)
154  {
156  return;
157  }
158  }
159 
160  if (Language != nullptr)
161  {
162  Language->SetAnsiMode(value);
163  }
164 
165  ansiMode = value;
166 }
167 
168 void Program::Exit()
169 {
171 }
void SetPrompt(const char *text) const
Definition: program.cpp:113
void NewPositionalInput(short unsigned int base, short unsigned int digits)
Definition: program.cpp:81
Master control class.
Definition: program.h:55
struct Number * ins
Definition: program.h:88
void NewPositionalOutput(short unsigned int base, short unsigned int digits)
Definition: program.cpp:97
PositionalNumeralSystem(unsigned int base, unsigned int digits, const char fractionPoint)
Definition: ntextp.cpp:40
virtual bool Keep()=0
virtual char GetFractionPoint()=0
int status
Definition: program.h:85
class FilesystemBase * Filesystem
Definition: program.h:74
void SetAnsiMode(bool value)
Definition: program.cpp:148
Definition: numb.h:66
virtual Number * Clone()=0
A list of user defined variables.
Definition: values.h:49
virtual bool SetAnsiMode(bool value)
Definition: console.cpp:109
virtual void Exit()
Definition: program.cpp:168
DecimalSystem(unsigned int digits, const char fractionPoint)
Definition: ntextd.cpp:71
bool GetAnsiMode() const
Definition: program.cpp:143
class FunctionList * Functions
Definition: program.h:78
Base class for all numeral systems with a positional notation.
Definition: ntextp.h:41
Represent a real number with 15 significant digits.
Definition: real.h:45
RealNumber()
Definition: real.cpp:37
void SetAnsiMode(bool value)
Definition: language.cpp:100
void InitAnsiMode()
Definition: program.cpp:138
void SetPrompt(const char *prompt)
virtual void Exit()=0
virtual void SetPrompt(const char *string)
Definition: console.cpp:214
virtual ~Program()
Definition: program.cpp:67
class VariableList * Variables
Definition: program.h:77
Program()
Definition: program.cpp:48
bool ansiMode
Definition: program.h:84
bool shellMode
Definition: program.h:83
struct Number * GetLastResult() const
Definition: program.cpp:122
int GetExitStatus() const
Definition: program.cpp:133
A list of user defined functions.
Definition: functionlist.h:42
void SetLastResult(Number *number)
Definition: program.cpp:127
class NumeralSystem * Input
Definition: program.h:75
int GetDigits() const
VariableList()
Definition: values.cpp:93
class ConsoleBase * Console
Definition: program.h:72
class PreferencesBase * Preferences
Definition: program.h:73
class NumeralSystem * Output
Definition: program.h:76