├── .github
├── FUNDING.yml
└── workflows
│ └── build-and-test.yml
├── .gitignore
├── CONTRIBUTING.md
├── LICENSE
├── Makefile
├── README.md
├── assets
├── panels.png
├── v1.0.png
├── v2.0-ss-colors.png
└── v2.0-ss.png
├── docs
└── pcalc.grammar
├── how-to-publish.md
├── include
├── draw.h
├── global.h
├── history.h
├── numberstack.h
├── operators.h
├── parser.h
└── xmalloc.h
├── run-tests.sh
├── src
├── draw.c
├── history.c
├── main.c
├── numberstack.c
├── operators.c
├── parser.c
└── xmalloc.c
└── tests
├── corner-cases.correct
├── corner-cases.test
├── expressions.correct
├── expressions.test
├── how-to-test.md
├── input-formats.correct
├── input-formats.test
├── number-bases.correct
├── number-bases.test
├── random.correct
└── random.test
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [alt-romes]
4 |
5 | #patreon: # Replace with a single Patreon username
6 | #open_collective: # Replace with a single Open Collective username
7 | #ko_fi: # Replace with a single Ko-fi username
8 | #tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
9 | #community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
10 | #liberapay: # Replace with a single Liberapay username
11 | #issuehunt: # Replace with a single IssueHunt username
12 | #otechie: # Replace with a single Otechie username
13 | #custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
14 |
--------------------------------------------------------------------------------
/.github/workflows/build-and-test.yml:
--------------------------------------------------------------------------------
1 | name: build-and-test
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | pull_request:
7 | branches: [ master ]
8 | workflow_dispatch:
9 |
10 | jobs:
11 | all:
12 | runs-on: ubuntu-latest
13 | steps:
14 | - uses: actions/checkout@v3
15 | - run: make
16 | - run: ./run-tests.sh
17 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.o
2 | *.a
3 | output/*
4 | pcalc
5 | .DS_Store
6 | .gitignore
7 | .vscode/*.json
8 | compile_flags.txt
9 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # How to contribute
2 |
3 | Hi! I'm happy you're interested in contributing. This project is still growing so we're trying to both better the current code, and adding interesting features.
4 |
5 | If you have any question or want to clarify something, write it out in the Discussions tab. We don't have a live chat channel but if proven needed we might make one.
6 |
7 |
8 | ## Submitting changes
9 |
10 | All changes must be submitted through pull requests.
11 |
12 | Before creating a pull request don't forget that:
13 |
14 | * The entire project should be recompiled with `make clean && make`
15 | * Changes must be documented so that reviewers and future contributors can understand the code
16 | * All existing tests should be run
17 | * The PR should have a simple and descriptive title
18 |
19 | ## Coding style
20 |
21 | * Indent using four spaces (soft tabs)
22 | * Please use standard snake_case variable names and functions in newly introduced code.
23 | * Functions, loops and if have a space between name and bracket (i.e. `while (i < 10) {`, `int func (int i) {`)
24 | * The curly bracket start on the same line as the declaration (i.e. `if (a == b) {*`)
25 | * When writing `else if`s, and `else`s, please write them one line after the closing `}`:
26 | ```
27 | if (...) {
28 | // ...
29 | }
30 | else {
31 | ...
32 | }
33 | ```
34 | * A space always comes after a comma (`int func (int x, int y, int z)`, not `int func (int x,int y,int z)`)
35 | * Between a variable and an operator is a space (i.e. `int i = 1;`, `i += 1;`)
36 | * When defining a pointer the `*` should stay close to the type (i.e. `int* i`, `char* str`), and when dereferencing the pointer the `*` should stay close to the variable name (i.e. `*i = 20`, `*str = 'a'`)
37 | * Consider the people who will read your code, and make it look nice for them :)
38 |
39 |
40 | ## Testing
41 |
42 | For information on testing please see [Testing](https://github.com/alt-romes/programmer-calculator/blob/master/tests/how-to-test.md)
43 |
44 |
45 | Thank you, ~romes
46 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | # https://github.com/danielpinto8zz6/c-cpp-project-generator#readme
2 |
3 | CC = gcc
4 | CFLAGS := -Wall -Wextra -g -Werror=missing-declarations -Werror=redundant-decls
5 | LFLAGS = -lncurses
6 | # OUTPUT := output
7 | SRC := src
8 | BUILDDIR := build
9 | BINDIR := bin
10 | INCLUDE := include
11 | # LIB := lib
12 |
13 | ifeq ($(OS),Windows_NT)
14 | MAIN := pcalc.exe
15 | SOURCEDIRS := $(SRC)
16 | INCLUDEDIRS := $(INCLUDE)
17 | # LIBDIRS := $(LIB)
18 | FIXPATH = $(subst /,\,$1)
19 | RM := del /q /f
20 | MD := mkdir
21 | else
22 | MAIN := pcalc
23 | SOURCEDIRS := $(shell find $(SRC) -type d)
24 | INCLUDEDIRS := $(shell find $(INCLUDE) -type d)
25 | # LIBDIRS := $(shell find $(LIB) -type d)
26 | FIXPATH = $1
27 | RM := rm -rf
28 | MD := mkdir -p
29 | CP := cp -i
30 | endif
31 |
32 | INCLUDES := $(patsubst %,-I%, $(INCLUDEDIRS:%/=%))
33 | # LIBS := $(patsubst %,-L%, $(LIBDIRS:%/=%))
34 | SOURCES := $(wildcard $(patsubst %,%/*.c, $(SOURCEDIRS)))
35 | OBJECTS := $(patsubst $(SOURCEDIRS)/%,$(BUILDDIR)/%,$(SOURCES:.c=.o))
36 |
37 | all: projdir $(MAIN)
38 | @echo Executing "all" complete!
39 |
40 | projdir:
41 | @$(MD) $(BUILDDIR)
42 | @$(MD) $(BINDIR)
43 |
44 | $(MAIN): $(OBJECTS)
45 | $(CC) $(CFLAGS) $(INCLUDES) -o $(BINDIR)/$(MAIN) $(OBJECTS) $(LFLAGS) # $(LIBS)
46 |
47 | $(BUILDDIR)/%.o: $(SOURCEDIRS)/%.c
48 | $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
49 |
50 | .PHONY: clean
51 | clean:
52 | $(RM) $(BINDIR)
53 | $(RM) $(BUILDDIR)
54 | @echo Cleanup complete!
55 |
56 | run: all
57 | $(BINDIR)/$(MAIN)
58 | @echo Executing "run: all" complete!
59 |
60 | .PHONY: install
61 | # Won't work for Windows Platform
62 | install:
63 | @echo "Installing!"
64 | $(MAKE) all
65 | @$(CP) $(BINDIR)/$(MAIN) /usr/local/bin
66 |
67 |
68 | .PHONY: uninstall
69 | uninstall:
70 | @echo "Unistalling :("
71 | @$(RM) $(shell whereis $(MAIN) | cut -d " " -f 2)
72 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Programmer calculator
2 |
3 | The programmer calculator is a simple terminal tool designed to give maximum efficiency and flexibility to the programmer working with:
4 |
5 | * binary, hexadecimal and decimal representations at the same time
6 | * bitwise operations
7 | * various operand sizes *(16bits, 32bits, 8bits, etc)*
8 |
9 | and who likes:
10 |
11 | * a clear, simple and customizable interface
12 | * open source software
13 | * terminal/cli tools
14 |
15 | 
16 | The above picture depicts `pcalc` without colors, and below is an example of `pcalc` with colors enabled (`--colors`) (which change depending on the terminal profile colors)
17 | 
18 |
19 | ## Making of
20 |
21 | The idea was born while developing a Nintendo Gameboy Emulator. Romes - the pitcher - found that the tools given online were clunky and did not allow for "nice multitasking"
22 |
23 | With the constant need to visualize and manipulate bits, it became evident that a better solution had to come to life
24 |
25 | ## Installation
26 |
27 | #### Homebrew
28 |
29 | Install from the homebrew official packages
30 | ```
31 | brew install pcalc
32 | ```
33 |
34 | #### Arch Based Distros
35 |
36 | Install from AUR
37 | ```
38 | yay -S programmer-calculator
39 | ```
40 |
41 | #### Building from Source (alternative)
42 |
43 | ##### Prerequisites:
44 | To build from source you need `gcc`, `ncurses`, and the source files.
45 | **If you don't have ncurses, please install it (i.e. with your system's package manager) first.**
46 | *(To install ncurses in Debian based distros run `sudo apt-get install libncurses5-dev libncursesw5-dev`)*
47 |
48 | ##### Building:
49 |
50 | First, clone the repository and change directory to it
51 | ```
52 | git clone https://github.com/alt-romes/programmer-calculator ; cd programmer-calculator
53 | ```
54 |
55 | Then, compile the code into an executable file and install it (installs in /usr/local/bin)
56 | ```
57 | sudo make install
58 | ```
59 |
60 | Conversely, if you ever want to uninstall, you can run:
61 | ```
62 | sudo make uninstall
63 | ```
64 |
65 | #### Updating
66 | Either re-build from source, or, using brew do
67 | ```
68 | brew update
69 | ```
70 | followed by
71 | ```
72 | brew upgrade pcalc
73 | ```
74 |
75 | #### Running
76 |
77 | Just run the programmer calculator program
78 | ```
79 | pcalc
80 | ```
81 |
82 | ## Features
83 |
84 | ### Usage
85 |
86 | There are various ways to insert values/operators, see the example `2 + 2` below:
87 |
88 | * `2`, followed by `+`, followed by `2`
89 | * `2`, followed by `+2`
90 | * `2+`, followed by `2`
91 | * `2+2` (or i.e. `2 + 2`)
92 |
93 | #### Inline Math
94 |
95 | Operator precedence and parenthesis for grouping is used.
96 |
97 | `2+2*3` evaluates to `8` and `(2+2)*3` evaluates to `12`
98 |
99 |
100 | ### Hex + Binary + Decimal
101 |
102 | All three number representations are available at the same time, you can insert `0xff + 0b101101 - 5` directly onto the calculator
103 |
104 |
105 | ### Operand Size
106 |
107 | By default, 64 bits are used for arithmetic, however, when working with bits, quite often we want to work with less. With this calculator you can change the amount of bits used. the number displayed will be unsigned
108 |
109 | To use 16 bits instead, type `16bit` (bits will also work)
110 |
111 | To use 8 bits, type `8bit`
112 |
113 | To use 0 < n <= 64 bits, type `nbit`
114 |
115 |
116 | ### Customizing Interface
117 |
118 | While running the calculator, you can type *what you see* for it to appear/disappear:
119 |
120 | `history` to toggle the history
121 | `decimal` to toggle the decimal representation
122 | `binary` to toggle the binary representation
123 | `hex` to toggle the hexadecimal representation
124 | `operation` to toggle the operation display
125 |
126 | Additionally, the interface colors can be toggled on and off.
127 |
128 | To set a default interface, define an alias for the program with the desired hidden options
129 | ```
130 | alias pcalc='pcalc -ibxdosn'
131 | ```
132 | i: history, b: binary, x: hex, d: decimal, o: operation, s: symbols, n: no colors
133 |
134 | You can also use the long options to hide parts: `--history`, `--decimal`, etc.
135 |
136 |
137 | ### Operations
138 | ```
139 | ADD + SUB - MUL * DIV /
140 | MOD % AND & OR | NOR $
141 | XOR ^ NOT ~ SL < SR >
142 | RL : RR ; 2's _ SE @
143 | ```
144 |
145 | * ADD: `a + b` arithmetic addition
146 | * SUB: `a - b` arithmetic subtraction
147 | * MUL: `a * b` arithmetic multiplication
148 | * DIV: `a / b` arithmetic integer division
149 | * MOD: `a % b` modulus from the division
150 | * AND: `a & b` bit-wise AND operation
151 | * OR : `a | b` bit-wise OR operation
152 | * NOR: `a $ b` bit-wise NOR operation : opposite of OR
153 | * XOR: `a ^ b` bit-wise XOR operation : exclusive OR
154 | * NOT: `~a` bit-wise NOT operation : change all bits of a, 0's into 1's and 1's into 0's
155 | * SL : `a < b` bit-wise SHIFT-LEFT operation : shift a left b number of times
156 | * SR : `a > b` bit-wise SHIFT-RIGHT operation : shift a right b number of times
157 | * RL : `a : b` bit-wise ROTATE-LEFT operation : rotate a left b number of times
158 | * RR : `a ; b` bit-wise ROTATE-RIGHT operation : rotate a right b number of times
159 | * 2's: `_a` 2's complement operation : 2's complement of a (usually is the symmetric of a)
160 | * SE : `@a` swap endianness : swap the byte order of a (uses the number of bits set by `bit` to determine the amount of bits swapped)
161 |
162 |
163 | ## Contributing
164 |
165 | Please reference [Contributing](https://github.com/alt-romes/programmer-calculator/blob/master/CONTRIBUTING.md)
166 |
167 |
168 | ---
169 |
170 | #### example usage in iterm panel
171 |
172 | 
173 |
174 |
--------------------------------------------------------------------------------
/assets/panels.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alt-romes/programmer-calculator/2787c7d4a80b103d6b5d3894abe9d533f5c58391/assets/panels.png
--------------------------------------------------------------------------------
/assets/v1.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alt-romes/programmer-calculator/2787c7d4a80b103d6b5d3894abe9d533f5c58391/assets/v1.0.png
--------------------------------------------------------------------------------
/assets/v2.0-ss-colors.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alt-romes/programmer-calculator/2787c7d4a80b103d6b5d3894abe9d533f5c58391/assets/v2.0-ss-colors.png
--------------------------------------------------------------------------------
/assets/v2.0-ss.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alt-romes/programmer-calculator/2787c7d4a80b103d6b5d3894abe9d533f5c58391/assets/v2.0-ss.png
--------------------------------------------------------------------------------
/docs/pcalc.grammar:
--------------------------------------------------------------------------------
1 | # This is just a normal text file
2 |
3 | Priorities:
4 |
5 | 1. parenthesis ((, )),
6 | 2. sign before a number (+, -)
7 | 3. bitwise not (~)
8 | 4. mult, div, remainder (*, /, %)
9 | 5. add, sub (+, -)
10 | 6. shifts and rotates (<<, >>, RoR, RoL)
11 | 7. bitwise and (&)
12 | 8. bitwise xor (^)
13 | 9. bitwiser or and nor (|, nor)
14 |
15 |
16 | Grammar:
17 |
18 | expression := or_exp
19 |
20 | or_exp := xor_exp ( (| | $) xor_exp )*
21 |
22 | xor_exp := and_exp (^ and_exp)*
23 |
24 | and_exp := shift_exp (& shift_exp)*
25 |
26 | shift_exp := add_exp ((<< | >> | ror | rol) add_exp)*
27 |
28 | add_exp := mult_exp ((+ | -) mult_exp)*
29 |
30 | mult_exp := not_exp ((* | / | %) not_exp)*
31 |
32 | prefix_exp: (~ | + | - | @)? atom_exp
33 |
34 | atom_exp: number | left_parenthesis expression right_parenthesis
35 |
36 | number: ( (0-9)+ | 0?x(0-9a-f)+ | 0?b(0-1)+ )
37 |
--------------------------------------------------------------------------------
/how-to-publish.md:
--------------------------------------------------------------------------------
1 | 0) Set the `VERSION` in `src/main.c`
2 | ```
3 | #define VERSION "va.b"
4 | ```
5 |
6 | 1) Set a tag for the version
7 | ```
8 | git tag va.b
9 |
10 | git push --tags
11 | ```
12 |
13 | 2) Edit the new tag on GitHub to make it a release with title and description
14 |
15 | 3) Calculate and copy the sha256sum for the newly released version from the github generated tarball: https://github.com/alt-romes/programmer-calculator/archive/va.b.tar.gz
16 | ```
17 | curl -L https://github.com/alt-romes/programmer-calculator/archive/va.b.tar.gz > va.b
18 |
19 | sha256sum va.b
20 | ```
21 |
22 | 5) Update formula in the official homebrew repository:
23 | `brew bump-formula-pr --version a.b pcalc`
24 |
25 | 6) Wait until homebrew-core accepts the pr, then test the updated brew formula by running
26 | ```
27 | brew update
28 |
29 | brew upgrade pcalc
30 | ```
31 |
32 | 8) Clone the AUR repository from https://aur.archlinux.org/packages/programmer-calculator/ (you must be a colaborator)
33 |
34 | 9) Edit `PKGBUILD`
35 | - change the version `pkgver` to `a.b`
36 | - update the sha sum to the one just calculated
37 |
38 | 10) Edit `.SRCINFO`
39 | - change the version `pkgver` to `a.b`
40 | - change in `source` references to previous version to the new version `a.b`
41 | - change the shasum
42 |
43 | 11) Push changes to the AUR repo
44 |
45 | 12) Test the AUR repo (howto?)
46 |
--------------------------------------------------------------------------------
/include/draw.h:
--------------------------------------------------------------------------------
1 | #ifndef _DRAW_H
2 | #define _DRAW_H
3 |
4 | #include
5 |
6 | #include "numberstack.h"
7 | #include "operators.h"
8 |
9 | enum colors {
10 |
11 | COLOR_PAIR_DEFAULT,
12 | COLOR_PAIR_OPERATION,
13 | COLOR_PAIR_DECIMAL,
14 | COLOR_PAIR_HEX,
15 | COLOR_PAIR_BINARY,
16 | COLOR_PAIR_BINARY_ALT,
17 | COLOR_PAIR_HISTORY,
18 | COLOR_PAIR_SYMBOLS,
19 | COLOR_PAIR_INPUT,
20 |
21 | };
22 |
23 | extern WINDOW* displaywin, *inputwin;
24 |
25 | extern int wMaxX, wMaxY;
26 | extern int operation_enabled, decimal_enabled, hex_enabled, ascii_enabled, symbols_enabled, binary_enabled, history_enabled, colors_enabled, alt_colors_enabled;
27 |
28 | extern int use_interface;
29 |
30 | void init_gui();
31 | void draw(numberstack*, operation*);
32 | void update_win_borders(numberstack* numbers);
33 | void sweepline(WINDOW*, int, int);
34 | void mvwprintw_colors(WINDOW* w, int y, int x, enum colors color_pair, const char* format, ...);
35 | void wprintw_colors(WINDOW* w, enum colors color_pair, const char* format, ...);
36 |
37 | #endif
38 |
--------------------------------------------------------------------------------
/include/global.h:
--------------------------------------------------------------------------------
1 | #ifndef _GLOBAL_H
2 | #define _GLOBAL_H
3 |
4 | #include
5 |
6 | #define MAX_IN 80
7 | #define INPUT_START 24
8 |
9 | #define MEM_FAIL -1
10 |
11 | void exit_pcalc(int);
12 |
13 | char *str_with_base_of_number(uint64_t, int type);
14 |
15 | #endif
16 |
--------------------------------------------------------------------------------
/include/history.h:
--------------------------------------------------------------------------------
1 | #ifndef _HISTORY_H
2 | #define _HISTORY_H
3 |
4 | #include
5 |
6 | #define HISTORY_RECORDS_BEFORE_REALLOC 20
7 |
8 | #define NTYPE_DEC 0
9 | #define NTYPE_HEX 1
10 | #define NTYPE_BIN 2
11 |
12 | struct history {
13 | int size;
14 | char **records;
15 | };
16 |
17 | extern struct history searchHistory;
18 | extern struct history history;
19 |
20 | void clear_history();
21 | void add_to_history(struct history* h,char* in);
22 | void add_number_to_history(uint64_t n, int type);
23 | void browsehistory(char*, int, int*);
24 | void free_history(struct history *h);
25 |
26 | #endif
27 |
--------------------------------------------------------------------------------
/include/numberstack.h:
--------------------------------------------------------------------------------
1 | #ifndef _NUMBERSTACK_H
2 | #define _NUMBERSTACK_H
3 |
4 | #include
5 |
6 | typedef struct numberstack {
7 | int max_size;
8 | int size;
9 | uint64_t * elements;
10 | } numberstack;
11 |
12 | extern numberstack* numbers;
13 |
14 | numberstack * create_numberstack(int max_size);
15 | uint64_t * pop_numberstack(numberstack* s);
16 | uint64_t * top_numberstack(numberstack* s);
17 | void push_numberstack(numberstack* s, uint64_t value);
18 | void clear_numberstack(numberstack* s);
19 | void free_numberstack(numberstack* s);
20 |
21 | #endif
22 |
--------------------------------------------------------------------------------
/include/operators.h:
--------------------------------------------------------------------------------
1 | #ifndef _OPERATORS_H
2 | #define _OPERATORS_H
3 |
4 | #include
5 |
6 | #define DEFAULT_MASK -1
7 | #define DEFAULT_MASK_SIZE 64
8 |
9 | /* Supress unused parameter warnings */
10 | #ifdef __GNUC__
11 | # define UNUSED(x) UNUSED_##x __attribute__((__unused__))
12 | #else
13 | # define UNUSED(x) UNUSED_##x
14 | #endif
15 |
16 | #define ALL_OPS "+-*/&|$^<>:;%~_@"
17 |
18 | #define OR_SYMBOL '|'
19 | #define NOR_SYMBOL '$'
20 | #define XOR_SYMBOL '^'
21 | #define AND_SYMBOL '&'
22 | #define SHR_SYMBOL '>'
23 | #define SHL_SYMBOL '<'
24 | #define ROR_SYMBOL ';'
25 | #define ROL_SYMBOL ':'
26 | #define MUL_SYMBOL '*'
27 | #define DIV_SYMBOL '/'
28 | #define MOD_SYMBOL '%'
29 | #define ADD_SYMBOL '+'
30 | #define SUB_SYMBOL '-'
31 | #define NOT_SYMBOL '~'
32 | #define TWOSCOMPLEMENT_SYMBOL '_'
33 | #define SWAPENDIANNESS_SYMBOL '@'
34 |
35 | // Operations Control
36 | // Example: '+' takes two operands, therefore the noperands = 2
37 | typedef struct operation {
38 | char character;
39 | unsigned char noperands;
40 | uint64_t (*execute) (uint64_t, uint64_t);
41 | } operation;
42 |
43 | extern uint64_t globalmask;
44 | extern int globalmasksize;
45 | extern operation *current_op;
46 |
47 | operation* getopcode(char c);
48 |
49 | uint64_t shr(uint64_t, uint64_t);
50 | uint64_t ror(uint64_t, uint64_t);
51 |
52 |
53 | #endif
54 |
--------------------------------------------------------------------------------
/include/parser.h:
--------------------------------------------------------------------------------
1 | #ifndef _PARSER_H
2 | #define _PARSER_H
3 |
4 | #include "operators.h"
5 |
6 | #define MAX_CHARS 80
7 |
8 | #define VALID_TOKENS "+-*/%&|$^~<>():;_@0123456789abcdefABCDEFx"
9 | #define VALID_NUMBER_INPUT "0123456789abcdefx()"
10 | #define VALID_DEC_SYMBOLS "0123456789"
11 | #define VALID_HEX_SYMBOLS "0123456789abcdefABCDEF"
12 | #define VALID_BIN_SYMBOLS "01"
13 |
14 | #define LPAR_SYMBOL '('
15 | #define RPAR_SYMBOL ')'
16 |
17 | #define OP_TYPE 0
18 | #define DEC_TYPE 1
19 | #define HEX_TYPE 2
20 | #define BIN_TYPE 3
21 |
22 | typedef struct exprtree {
23 | int type;
24 | union {
25 | operation* op;
26 | uint64_t* value;
27 | };
28 | struct exprtree* left;
29 | struct exprtree* right;
30 | } * exprtree;
31 |
32 | typedef struct parser_t {
33 | char* tokens;
34 | int ntokens;
35 | int pos;
36 | } * parser_t;
37 |
38 | char* sanitize(const char*);
39 | exprtree parse(char*);
40 | uint64_t calculate(exprtree);
41 | void free_exprtree(exprtree);
42 |
43 | extern int total_trees_created;
44 | extern int total_trees_freed;
45 | extern int total_parsers_created;
46 | extern int total_parsers_freed;
47 | extern int total_tokens_created;
48 | extern int total_tokens_freed;
49 |
50 | #endif
51 |
--------------------------------------------------------------------------------
/include/xmalloc.h:
--------------------------------------------------------------------------------
1 | #ifndef _XMALLOC_H
2 | #define _XMALLOC_H
3 |
4 | #include
5 |
6 | #include "global.h"
7 |
8 | void* xmalloc(size_t bytes);
9 | void* xmalloc_with_ressources(size_t bytes, void** ressources, size_t nres);
10 | void* xcalloc(size_t nelem, size_t bytes);
11 | void* xcalloc_with_ressources(size_t nelem, size_t bytes, void** ressources, size_t nres);
12 | void* xrealloc(void* pntr, size_t bytes);
13 | void* xrealloc_with_ressources(void* pntr, size_t bytes, void** ressources, size_t nres);
14 | void xfreen(void** pntrs, size_t npntrs);
15 | void xfree(void* pntr);
16 |
17 | #endif
18 |
--------------------------------------------------------------------------------
/run-tests.sh:
--------------------------------------------------------------------------------
1 | tests=( "number-bases" "random" "expressions" "input-formats" "corner-cases" )
2 | for t in "${tests[@]}"
3 | do
4 | diff -b tests/$t.correct <(cat tests/$t.test | bin/pcalc -n) ||
5 | if echo "Test failed:"; then
6 | echo tests/$t
7 | exit 1
8 | fi
9 | done
10 | echo "All tests passed"
11 |
--------------------------------------------------------------------------------
/src/draw.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include "draw.h"
6 | #include "history.h"
7 | #include "numberstack.h"
8 | #include "operators.h"
9 | #include "parser.h"
10 |
11 | WINDOW* displaywin, * inputwin;
12 |
13 | // ASCII control characters
14 | const char* ctrl_chars[] = { "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK",
15 | "BEL", "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI", "DLE", "DC1", "DC2",
16 | "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC", "FS", "GS",
17 | "RS", "US", "SPACE"};
18 |
19 | int wMaxX;
20 | int wMaxY;
21 |
22 | int operation_enabled = 1;
23 | int decimal_enabled = 1;
24 | int hex_enabled = 1;
25 | int ascii_enabled = 1;
26 | int symbols_enabled = 1;
27 | int binary_enabled = 1;
28 | int history_enabled = 1;
29 | int colors_enabled = 0;
30 | int alt_colors_enabled = 0;
31 |
32 | int use_interface = 1;
33 |
34 | static void printbinary(uint64_t, int);
35 | static void printhistory(numberstack*, int);
36 |
37 | void init_gui() {
38 |
39 | if (use_interface) {
40 |
41 | initscr();
42 | /* Only use colors if set so and if available */
43 | if (colors_enabled && has_colors() == true) {
44 | start_color();
45 | /* Every color pair needs to be initalized before use */
46 | init_pair(COLOR_PAIR_OPERATION, COLOR_YELLOW, COLOR_BLACK);
47 | init_pair(COLOR_PAIR_DECIMAL, COLOR_CYAN, COLOR_BLACK);
48 | init_pair(COLOR_PAIR_HEX, COLOR_MAGENTA, COLOR_BLACK);
49 | init_pair(COLOR_PAIR_BINARY, COLOR_CYAN, COLOR_BLACK);
50 | init_pair(COLOR_PAIR_BINARY_ALT, COLOR_MAGENTA, COLOR_BLACK);
51 | init_pair(COLOR_PAIR_SYMBOLS, COLOR_YELLOW, COLOR_BLACK);
52 | init_pair(COLOR_PAIR_HISTORY, COLOR_MAGENTA, COLOR_BLACK);
53 | init_pair(COLOR_PAIR_INPUT, COLOR_YELLOW, COLOR_BLACK);
54 | } else {
55 | /* Disable colors if terminal does not support colors */
56 | colors_enabled = 0;
57 | }
58 | cbreak();
59 |
60 | getmaxyx(stdscr, wMaxY, wMaxX);
61 |
62 | displaywin = newwin(wMaxY-3, wMaxX, 0, 0);
63 | refresh();
64 |
65 | box(displaywin, ' ', 0);
66 | if (symbols_enabled) {
67 |
68 | mvwprintw_colors(displaywin, wMaxY-8, 2, COLOR_PAIR_SYMBOLS, "ADD + SUB - MUL * DIV /\n");
69 | wprintw_colors(displaywin, COLOR_PAIR_SYMBOLS, " MOD %% AND & OR | NOR $\n");
70 | wprintw_colors(displaywin, COLOR_PAIR_SYMBOLS, " XOR ^ NOT ~ SL < SR >\n");
71 | wprintw_colors(displaywin, COLOR_PAIR_SYMBOLS, " RL : RR ; 2's _ SE @");
72 | }
73 | wrefresh(displaywin);
74 | inputwin = newwin(3, wMaxX, wMaxY-3, 0);
75 | refresh();
76 | box(inputwin, ' ', 0);
77 | wrefresh(inputwin);
78 |
79 | }
80 |
81 | }
82 |
83 | static void printbinary(uint64_t value, int priority) {
84 |
85 | uint64_t mask = ((uint64_t) 1) << (globalmasksize - 1); // Mask starts at the last bit to display, and is >> until the end
86 |
87 | int i=DEFAULT_MASK_SIZE-globalmasksize;
88 |
89 | mvwprintw_colors(displaywin, 8-priority, 2, COLOR_PAIR_BINARY, "Binary: \n %02d ", globalmasksize); // %s must be a 2 digit number
90 |
91 | for (; i<64; i++, mask>>=1) {
92 |
93 | uint64_t bitval = value & mask;
94 | wprintw_colors(displaywin,
95 | (alt_colors_enabled && bitval) ? COLOR_PAIR_BINARY_ALT : COLOR_PAIR_BINARY,
96 | "%c", bitval ? '1' : '0');
97 |
98 | if (i%16 == 15 && 64 - ((i/16+1)*16))
99 | // TODO: Explain these numbers better (and decide if to keep them)
100 | wprintw_colors(displaywin, COLOR_PAIR_BINARY,"\n %d ", 64-((i/16)+1)*16);
101 | else if (i%8 == 7)
102 | wprintw_colors(displaywin, COLOR_PAIR_BINARY, " ");
103 | else if (i%4 == 3)
104 | wprintw_colors(displaywin, COLOR_PAIR_BINARY, " ");
105 | else
106 | waddch(displaywin, ' ');
107 |
108 | }
109 | }
110 |
111 | static void printhistory(numberstack* numbers, int priority) {
112 | int currY,currX;
113 | mvwprintw_colors(displaywin, 14-priority, 2, COLOR_PAIR_HISTORY, "History: ");
114 | for (int i=0; i= wMaxX-3 || currY > 14) {
117 | clear_history();
118 | uint64_t aux = *top_numberstack(numbers);
119 | add_number_to_history(aux, 0);
120 | }
121 | wprintw_colors(displaywin, COLOR_PAIR_HISTORY, "%s ", history.records[i]);
122 | }
123 | }
124 |
125 | static void display_ascii_hex(uint64_t value, int priority) {
126 | // ASCII not enabled, just display HEX
127 | // Or ASCII out of range
128 | if ((hex_enabled && !ascii_enabled) || value > 127) {
129 | mvwprintw_colors(displaywin, priority, 2, COLOR_PAIR_HEX, "Hex: 0x%llX", value);
130 | return;
131 | }
132 |
133 | // HEX not enabled, just display ASCII
134 | if (!hex_enabled && ascii_enabled) {
135 | // Display control characters
136 | if (value < 33) {
137 | mvwprintw_colors(displaywin, priority, 2, COLOR_PAIR_HEX, "ASCII: %s", ctrl_chars[value]);
138 | }
139 | // Display DEL (Dec: 127)
140 | else if (value == 127) {
141 | mvwprintw_colors(displaywin, priority, 2, COLOR_PAIR_HEX, "ASCII: DEL");
142 | }
143 | // Display printable characters
144 | else {
145 | mvwprintw_colors(displaywin, priority, 2, COLOR_PAIR_HEX, "ASCII: %c", (uint8_t)value);
146 | }
147 | return;
148 | }
149 |
150 | // Both ASCII and HEX enabled
151 | // Display control characters
152 | if (value < 33) {
153 | mvwprintw_colors(displaywin, priority, 2, COLOR_PAIR_HEX, "Hex: 0x%llX ASCII: %s", value, ctrl_chars[value]);
154 | }
155 | // Display DEL (Dec: 127)
156 | else if (value == 127) {
157 | mvwprintw_colors(displaywin, priority, 2, COLOR_PAIR_HEX, "Hex: 0x7F ASCII: DEL");
158 | }
159 | else {
160 | // Display printable characters
161 | mvwprintw_colors(displaywin, priority, 2, COLOR_PAIR_HEX, "Hex: 0x%llX ASCII: %c", value, (uint8_t)value);
162 | }
163 | }
164 |
165 |
166 | void draw(numberstack* numbers, operation* current_op) {
167 |
168 | uint64_t* np = top_numberstack(numbers);
169 | uint64_t n;
170 |
171 | if (np == NULL) n = 0;
172 | else n = *np;
173 |
174 | if (use_interface) {
175 |
176 | int prio = 0; // Priority
177 |
178 | // Clear lines
179 | for(int i = 2 ; i < 16 ; i++) {
180 | sweepline(displaywin, i, 0);
181 | }
182 |
183 | if(!operation_enabled) prio += 2;
184 | else mvwprintw_colors(displaywin, 2, 2, COLOR_PAIR_OPERATION, "Operation: %c\n", current_op ? current_op->character : ' ');
185 |
186 | if(!decimal_enabled) prio += 2;
187 | else mvwprintw_colors(displaywin, 4-prio, 2, COLOR_PAIR_DECIMAL, "Decimal: %lld", (long long)n);
188 |
189 | if(!hex_enabled && !ascii_enabled) prio += 2;
190 | else display_ascii_hex(n, 6-prio);
191 |
192 | if(!binary_enabled) prio +=6;
193 | else printbinary(n,prio);
194 |
195 | if(!history_enabled) prio += 2;
196 | else printhistory(numbers,prio);
197 |
198 | wrefresh(displaywin);
199 |
200 | // Clear input
201 | sweepline(inputwin, 1, 19);
202 |
203 | // Prompt input
204 | mvwprintw_colors(inputwin, 1, 2, COLOR_PAIR_INPUT, "Number or operator: ");
205 | wrefresh(inputwin);
206 |
207 | }
208 | else {
209 |
210 | printf("Decimal: %lld, Hex: 0x%llx, Operation: %c\n", (long long)n, (unsigned long long)n, current_op ? current_op->character : ' ');
211 | /* printf("created|freed -> tokens: %d|%d, parsers: %d|%d, trees: %d|%d\n", total_tokens_created, total_tokens_freed, total_parsers_created, total_parsers_freed, total_trees_created, total_trees_freed); */
212 | }
213 | }
214 |
215 | void mvwprintw_colors(WINDOW* w, int y, int x, enum colors color_pair, const char* format, ...) {
216 | /* Prints colors if available otherwise not */
217 | va_list ap;
218 | va_start(ap, format);
219 | wmove(w, y, x);
220 | wattron(w, COLOR_PAIR(color_pair));
221 | vw_printw(w, format, ap);
222 | wattroff(w, COLOR_PAIR(color_pair));
223 | va_end(ap);
224 |
225 | }
226 |
227 | void wprintw_colors(WINDOW* w, enum colors color_pair, const char* format, ...) {
228 | /* Prints colors if available otherwise not */
229 | va_list ap;
230 | va_start(ap, format);
231 | wattron(w, COLOR_PAIR(color_pair));
232 | vw_printw(w, format, ap);
233 | wattroff(w, COLOR_PAIR(color_pair));
234 | va_end(ap);
235 | }
236 |
237 | void update_win_borders(numberstack* numbers) {
238 |
239 | doupdate();
240 | init_gui();
241 | draw(numbers, current_op);
242 | }
243 |
244 |
245 | void sweepline(WINDOW* w, int y, int x) {
246 | wmove(w, y, x);
247 | wclrtoeol(w);
248 | }
249 |
--------------------------------------------------------------------------------
/src/history.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include "draw.h"
6 | #include "global.h"
7 | #include "history.h"
8 | #include "xmalloc.h"
9 |
10 | struct history searchHistory;
11 | struct history history;
12 |
13 | void clear_history() {
14 |
15 | for (; history.size>0; history.size--)
16 | free(history.records[history.size-1]);
17 |
18 | free(history.records);
19 | // To make sure realloc behaves like malloc later
20 | history.records = NULL;
21 |
22 | sweepline(displaywin, 14, 11);
23 | sweepline(displaywin, 15, 0);
24 | }
25 |
26 | void add_to_history(struct history* h, char* in) {
27 |
28 | if (h->size % HISTORY_RECORDS_BEFORE_REALLOC == 0)
29 | h->records = xrealloc(h->records, (h->size + HISTORY_RECORDS_BEFORE_REALLOC) * sizeof(char *));
30 |
31 | if ((h->records[h->size++] = strdup(*in == '\0' && h == &history ? "0" : in)) == NULL)
32 | exit_pcalc(MEM_FAIL);
33 |
34 | }
35 |
36 | void add_number_to_history(uint64_t n, int type) {
37 |
38 | char *str = str_with_base_of_number(n, type);
39 | add_to_history(&history, str);
40 | xfree(str);
41 | }
42 |
43 | void browsehistory(char* in , int mode, int* counter) {
44 |
45 | /* @mode is -1 when scrolling up
46 | * @mode is 1 when scrolling down
47 | * this is due to the fact that when scrolling up we're browsing the history backwards,
48 | * starting at the most recent command, until we hit the oldest command added to history
49 | */
50 |
51 | if( (mode == 1 && *counter < searchHistory.size-1) || (mode == -1 && *counter > 0)) {
52 |
53 | *counter += mode;
54 | strcpy(in, searchHistory.records[*counter]);
55 | }
56 | else if (mode == 1 && *counter == searchHistory.size - 1) {
57 |
58 | // When the user is scrolling down and the limit is reached, the input becomes empty again, and the counter is set to the end
59 |
60 | *counter += 1; /* Set the counter == searchHistory.size.
61 | * this is a non existent position, indicating that the counter
62 | * is currently not being used.
63 | *
64 | * You can also think about it in this way:
65 | * Next time the user presses key up, the counter == searchHistory.size
66 | * will be decremented, and the last position of history will be accessed
67 | * history[searchHistory.size - 1]
68 | */
69 | strcpy(in, "");
70 | }
71 |
72 | }
73 |
74 | void free_history(struct history *h) {
75 |
76 | for (int i = 0; i < h->size; ++i)
77 | xfree(h->records[i]);
78 |
79 | xfree(h->records);
80 |
81 | }
82 |
83 |
84 | char *str_with_base_of_number(uint64_t n, int type) {
85 |
86 | char *str = xmalloc(67);
87 |
88 | if (type == 0)
89 | sprintf(str,"%llu", (unsigned long long)n);
90 | else if (type == 1)
91 | sprintf(str,"0x%llX", (unsigned long long)n);
92 | else if (type == 2) {
93 |
94 | uint64_t mask = ror(1, 1);
95 |
96 | int i = 0;
97 | for (; i<64; i++, mask>>=1)
98 | if (mask & n)
99 | break;
100 |
101 | int nbits = globalmasksize - i;
102 |
103 | sprintf(str, "0b");
104 | if (nbits == 0)
105 | str[2] = '0';
106 | else
107 | for (i=0; i>=1)
108 | str[i+2] = mask & n ? '1' : '0';
109 |
110 | str[i+2] = '\0';
111 | }
112 |
113 | return str;
114 |
115 | }
116 |
--------------------------------------------------------------------------------
/src/main.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | #include "global.h"
11 | #include "draw.h"
12 | #include "history.h"
13 | #include "numberstack.h"
14 | #include "operators.h"
15 | #include "parser.h"
16 |
17 |
18 |
19 |
20 |
21 | #define VERSION "v3.0"
22 |
23 |
24 |
25 |
26 |
27 | /*---- Function Prototypes ----------------------------------------*/
28 |
29 |
30 | static void process_prompt(operation**, char*);
31 | static void get_input(char*);
32 | static void apply_operations(numberstack*, operation**);
33 | static void exit_pcalc_success();
34 |
35 |
36 |
37 |
38 |
39 | /*---- Main Logic -------------------------------------------------*/
40 |
41 |
42 | int main(int argc, char* argv[])
43 | {
44 | // Set all long arguments that can be used
45 | struct option long_options[] = {
46 |
47 | {"help", no_argument, NULL, 'h'},
48 | {"version", no_argument, NULL, 'v'},
49 | {"history", no_argument, NULL, 'i'},
50 | {"binary", no_argument, NULL, 'b'},
51 | {"hex", no_argument, NULL, 'x'},
52 | {"ascii", no_argument, NULL, 'A'},
53 | {"decimal", no_argument, NULL, 'd'},
54 | {"operation", no_argument, NULL, 'o'},
55 | {"symbol", no_argument, NULL, 's'},
56 | {"colors", no_argument, NULL, 'c'},
57 | {"alternate-colors", no_argument, NULL, 'a'},
58 | {"no-interface", no_argument, NULL, 'n'},
59 | {NULL, 0, NULL, 0}
60 |
61 | };
62 |
63 | // Get command line options to hide parts of the display
64 | int opt;
65 | while ((opt = getopt_long(argc, argv, "hvibxdoscan", long_options, NULL)) != -1) {
66 | switch (opt) {
67 |
68 | case 'h':
69 | puts("Currently --help only displays the following information about the program options.");
70 | puts("If you think something else should be here let us know @ github.com/alt-romes/programmer-calculator");
71 | puts("The following options customize the interface: -ibxdos");
72 | puts("--history = -i\t\t\tdisables command history");
73 | puts("--binary = -b\t\t\tdisables binary representation");
74 | puts("--hex = -x\t\t\tdisables hexadecimal representation");
75 | puts("--ascii = -A\t\t\tdisables ASCII representation");
76 | puts("--decimal = -d\t\t\tdisables decimal representation");
77 | puts("--operation = -o\t\tdisables the display of the current operation");
78 | puts("--symbol = -s\t\t\tdisables the display of helper command symbols");
79 | puts("Other options:");
80 | puts("--colors = -c\t\t\tenables colors");
81 | puts("--alternate-colors = -a \tenables alternate colors for 1s and 0s in binary");
82 | puts("--no-interface = -n\t\tdisables graphical interface");
83 | exit(0);
84 | break;
85 |
86 | case 'v':
87 | printf("Programmer calculator %s\n", VERSION);
88 | exit(0);
89 | break;
90 |
91 | case 'i':
92 | history_enabled = 0;
93 | break;
94 |
95 | case 'b':
96 | binary_enabled = 0;
97 | break;
98 |
99 | case 'x':
100 | hex_enabled = 0;
101 | break;
102 |
103 | case 'A':
104 | ascii_enabled = 0;
105 | break;
106 |
107 | case 'd':
108 | decimal_enabled = 0;
109 | break;
110 |
111 | case 'o':
112 | operation_enabled = 0;
113 | break;
114 |
115 | case 's':
116 | symbols_enabled = 0;
117 | break;
118 |
119 | case 'n':
120 | use_interface = 0;
121 | break;
122 |
123 | case 'c':
124 | colors_enabled = 1;
125 | break;
126 |
127 | case 'a':
128 | colors_enabled = 1;
129 | alt_colors_enabled = 1;
130 | break;
131 |
132 | default:
133 | exit(EXIT_FAILURE);
134 | }
135 | }
136 |
137 |
138 | init_gui(&displaywin, &inputwin);
139 |
140 | // Set handler for CTRL+C to clean exit
141 | signal(SIGINT, exit_pcalc_success);
142 |
143 | /*
144 | * The numberstack is used to store numbers used in calculations
145 | * It's a normal stack data structure (LIFO) that holds uint64_t integers
146 | * Check the stack.h file for its operations
147 | *
148 | * The operation structure holds information regarding the ASCII character the operation uses,
149 | * The number of operands the operation takes, and the function to execute when applied
150 | *
151 | * Numbers get pushed to the numberstack "numbers", and operations are set as the "current_op"
152 | *
153 | * After receiving user input, if there's a current operation, the program compares the
154 | * current stack size to the number of operands needed for that operation
155 | *
156 | * Should the operation be executed, the needed operands are popped from the stack,
157 | * the operation is executed, and the result of the calculation is pushed to the stack
158 | */
159 | numbers = create_numberstack(4);
160 | //operation* current_op = NULL;
161 |
162 | // Initalize history pointers with NULL (realloc will bahave like malloc)
163 | history.records = NULL;
164 | searchHistory.records = NULL;
165 |
166 | // Start numberstack and history with 0
167 | push_numberstack(numbers, 0);
168 | add_to_history(&history, "0");
169 | // Display number on top of the stack (0)
170 | draw(numbers, current_op);
171 |
172 | // No longer add empty string to history bottom, because the scroll was reversed
173 | /* add_to_history(&searchHistory, ""); */
174 |
175 | //Main Loop
176 | for (;;) {
177 |
178 | // Get input
179 | char in[MAX_IN + 1];
180 |
181 | // Make sure that if enter is pressed, a len == 0 null terminated string is in "in"
182 | in[0] = '\0';
183 |
184 | get_input(in);
185 |
186 | process_prompt(¤t_op, in);
187 |
188 | // Display number on top of the stack
189 | draw(numbers, current_op);
190 | }
191 |
192 | endwin();
193 |
194 | return 0;
195 | }
196 |
197 | static void process_prompt(operation** current_op, char* prompt) {
198 |
199 | // Process input
200 |
201 | // Try to find a known command and handle it
202 | if (!strcmp(prompt, "quit") || !strcmp(prompt, "q") || !strcmp(prompt, "exit"))
203 | exit_pcalc(0);
204 |
205 | else if (!strcmp(prompt, "binary"))
206 | binary_enabled = !binary_enabled;
207 |
208 | else if (!strcmp(prompt, "hex"))
209 | hex_enabled = !hex_enabled;
210 |
211 | else if (!strcmp(prompt, "ascii"))
212 | ascii_enabled = !ascii_enabled;
213 |
214 | else if (!strcmp(prompt, "decimal"))
215 | decimal_enabled = !decimal_enabled;
216 |
217 | else if (!strcmp(prompt, "history"))
218 | history_enabled = !history_enabled;
219 |
220 | else if (!strcmp(prompt, "operation"))
221 | operation_enabled = !operation_enabled;
222 |
223 | else if (strstr(prompt, "bit") != NULL) {
224 |
225 | // Command to change the number of bits
226 |
227 | int requestedmasksize = atoi(prompt);
228 | globalmasksize = requestedmasksize > DEFAULT_MASK_SIZE || requestedmasksize <= 0 ? DEFAULT_MASK_SIZE : requestedmasksize;
229 |
230 | //globalmask cant be 0x16f's
231 | globalmask = shr((DEFAULT_MASK_SIZE-globalmasksize), DEFAULT_MASK);
232 |
233 | // apply mask to all numbers in stack
234 | numberstack* aux = create_numberstack(numbers->size);
235 | for (int i=0; isize; i++)
236 | push_numberstack(aux, *pop_numberstack(numbers) & globalmask);
237 |
238 | for (int i=0; isize; i++)
239 | push_numberstack(numbers, *pop_numberstack(aux) & globalmask);
240 |
241 | }
242 |
243 | else {
244 |
245 | // It's not a known command - handle input as expression
246 |
247 |
248 | // Remove any unknown characters from prompt
249 | char* input = sanitize(prompt);
250 |
251 | // We need to check if the last token is an operation before it gets freed,
252 | // And save it, to set it as the current op after the input is processed
253 | operation* suffix_op = NULL;
254 |
255 |
256 | // Search for an operation symbol as the first token
257 |
258 | /* There are four valid situations when an operation symbol
259 | * is found in the tokens
260 | *
261 | * 1 - just the op i.e. "+"
262 | * 2 - an expression ending with an op i.e. "2+"
263 | * 3 - an op then an expression i.e. "+2"
264 | * 4 - an expression i.e. "1+2*3" (this case is handled as a number)
265 | */
266 |
267 | int inputlen = strlen(input);
268 |
269 | if (input[0] != '\0' && strchr(ALL_OPS, input[0]) &&
270 | (inputlen == 1 || (input[0] != NOT_SYMBOL && input[0] != TWOSCOMPLEMENT_SYMBOL && input[0] != SWAPENDIANNESS_SYMBOL))) {
271 |
272 | // The input is either just an op, or an expression that starts with an op that isn't a prefix | case 1 or case 3
273 |
274 | // Set the current operation as the operation structure for that symbol
275 | *current_op = getopcode(input[0]);
276 |
277 | // Add the operation to history
278 | char opchar[2] = {input[0], '\0'};
279 | add_to_history(&history, opchar);
280 |
281 | // Duplicate the *tokens* string starting from the immediate next position, and free previous tokens afterwards
282 | char* tokens_wout_op = strdup(input+1);
283 |
284 | free(input);
285 |
286 | input = tokens_wout_op;
287 |
288 | // The length of the input is now 1 character smaller
289 | inputlen--;
290 |
291 | }
292 |
293 | if (inputlen > 0 && strchr(ALL_OPS, input[inputlen-1])) {
294 |
295 | // Last char is an op | case 2
296 |
297 | // Set a new operation from the last symbol
298 | suffix_op = getopcode(input[inputlen-1]);
299 |
300 | // Remove the last token from the string
301 | input[inputlen-1] = '\0';
302 | inputlen--;
303 | }
304 |
305 | if (*current_op == NULL ||
306 | (prompt[0] == '\0' && !(*current_op = NULL))) {
307 |
308 | // There's no current operation and we're going to process a new number
309 | // -> clear the stack and history before processing it
310 | // Or the input was empty. When the input is empty set the operation to NULL
311 |
312 | clear_numberstack(numbers);
313 | clear_history();
314 |
315 | }
316 |
317 | if (inputlen > 0) {
318 |
319 | // Add the tokens to history as a whole, for now...
320 | add_to_history(&history, input);
321 |
322 | // Parse the tokens into an expression
323 | // This function will free *tokens*
324 | exprtree expression = parse(input);
325 |
326 | // Calculate the result of the expression
327 | // The globalmask is applied inside calculate
328 | uint64_t result = calculate(expression);
329 |
330 | // The expression is no longer needed since we have its value
331 | free_exprtree(expression);
332 |
333 | // Push result to the numberstack
334 | push_numberstack(numbers, result);
335 |
336 | if (suffix_op != NULL) {
337 |
338 | // Last token is an op | case 2
339 |
340 | // Apply pending operation right away, to then set a new one
341 | apply_operations(numbers, current_op);
342 |
343 | // Set a new operation from the symbol
344 | *current_op = suffix_op;
345 |
346 | char opchar[2] = {suffix_op->character, '\0'};
347 | add_to_history(&history, opchar);
348 | }
349 |
350 | }
351 | else {
352 | // The input expression generated an empty token string.
353 | // Because parse() isn't called, we must free *tokens* manually
354 | free(input);
355 | total_tokens_freed++;
356 | }
357 |
358 | if (inputlen == 0 && *current_op == NULL) {
359 |
360 | // The op is null (means we cleared the stack before reading a number)
361 | // But we didn't read a number - so the stack is empty
362 |
363 | // Add needed 0 to history and to stack
364 | push_numberstack(numbers, 0);
365 | add_to_history(&history, "0");
366 |
367 | }
368 |
369 | }
370 |
371 | // Apply operations
372 | apply_operations(numbers, current_op);
373 |
374 | }
375 |
376 |
377 | static void apply_operations(numberstack* numbers, operation** current_op) {
378 |
379 | if (*current_op != NULL) {
380 |
381 | unsigned char noperands = (*current_op)->noperands;
382 |
383 | if (numbers->size >= noperands) {
384 |
385 | uint64_t operands[2] = {0};
386 |
387 | for (unsigned char i=0; i < noperands; i++)
388 | operands[i] = *pop_numberstack(numbers);
389 |
390 | uint64_t result = (*current_op)->execute(operands[0], operands[1]) & globalmask;
391 |
392 | push_numberstack(numbers, result);
393 |
394 | *current_op = NULL; // Set to invalid operation
395 | }
396 | }
397 |
398 | }
399 |
400 |
401 | static void get_input(char* in) {
402 |
403 | char inp;
404 | int history_counter = searchHistory.size;
405 |
406 | // Is the cursor at the end of the line or somewhere in the middle
407 | int browsing = 0;
408 |
409 | // Collect input until enter is pressed
410 | for (int pos = 0, len = 0; (inp = getchar()) != 13 && inp != '\n';) {
411 |
412 | // Get max possible input length
413 | int max = getmaxx(inputwin) - INPUT_START;
414 |
415 | int searched = 0;
416 |
417 | /* Check for forbidden keys
418 | * -1 is a key that indicates the terminal got resized
419 | * 5 is a key that indicates mouse wheel down
420 | * 25 is a key that indicates mouse wheel up
421 | * 27 is a key that indicates an arrow key was pressed
422 | * 127 is a key that indicates the brackspace key was pressed
423 | */
424 | switch(inp) {
425 |
426 | case -1:
427 | update_win_borders(numbers);
428 | case 25:
429 | continue;
430 | break;
431 |
432 | case 1:
433 | //CTRL-A
434 | pos = 0;
435 | browsing = searched = 1;
436 | break;
437 | case 5:
438 | //CTRL-E
439 | pos = len;
440 | browsing = 0;
441 | searched = 1;
442 | break;
443 |
444 | case 4:
445 | //CTRL-D
446 | exit_pcalc(0);
447 | break;
448 |
449 | case 12:
450 | //CTRL-L
451 | clear_history();
452 | pos = len = 0;
453 | in[0] = '\0';
454 | return;
455 | break;
456 |
457 | case 8:
458 | //CTRL-Backspace
459 | case 23:
460 | //CTRL-W
461 | inp = '\0';
462 | if(pos == 0)
463 | continue;
464 |
465 | int jump = 0; //Amount of characters removed
466 | while(in[pos-1] == ' ' && pos > 0) { //Delete trailing spaces
467 | pos--; len--; //Delete 1 character
468 | jump++;
469 | }
470 | while(in[pos-1] != ' ' && pos > 0) { //Delete last typed word
471 | pos--; len--;
472 | jump++;
473 | }
474 |
475 | if(browsing) {
476 | for (int i = pos; i <= len + jump; i++) {
477 | in[i] = in[i + jump - 1];
478 | }
479 | }
480 |
481 | in[len + 1] = '\0';
482 |
483 | break;
484 |
485 | case 27:
486 | getchar();
487 | inp = getchar();
488 | switch (inp) {
489 |
490 | case 'A':
491 | // Up arrow
492 | browsehistory(in, -1, &history_counter);
493 | len = strlen(in);
494 | searched = 1;
495 | browsing = 0;
496 |
497 | break;
498 |
499 | case 'B':
500 | // Down arrow
501 | browsehistory(in, 1, &history_counter);
502 | len = strlen(in);
503 | searched = 1;
504 | browsing = 0;
505 |
506 | break;
507 |
508 | case 'C':
509 | // Right arrow
510 | if (browsing) { // The right arrow should only work while in the middle of the input
511 | pos++;
512 |
513 | // Exit browsing mode if the cursor is at the end of the input
514 | if (pos == len) {
515 | browsing = 0;
516 | }
517 | }
518 | searched = 1;
519 |
520 | break;
521 |
522 | case 'D':
523 | // Left arrow
524 | if (pos != 0) {
525 | pos--;
526 | browsing = 1; // The left arrow will always be in browsing mode, so no need for a check
527 | }
528 | searched = 1;
529 |
530 | break;
531 | }
532 | break;
533 |
534 | case 127:
535 | // Backspace
536 |
537 | if (pos != 0) {
538 | pos--;
539 | len--;
540 | inp = '\0';
541 | }
542 | else {
543 | // Skip printing if backspace was pressed but nothing was done, otherwise a strange undefined character is printed
544 | searched = 1;
545 | }
546 | break;
547 |
548 | }
549 |
550 | // Prevent user to input more than MAX_IN
551 | if(!searched && len <= MAX_IN && (len <= max || !use_interface)) {
552 | if (!browsing) {
553 | // If the cursor is at the end of the text
554 |
555 | // Append char to in array
556 | in[pos] = inp;
557 | in[++pos] = '\0';
558 | len++; // Make sure that len is still equal to pos
559 |
560 | if (inp == '\0') {
561 | // Clear screen from previous input
562 | sweepline(inputwin, 1, 22 + --len);
563 | }
564 | }
565 | else {
566 | // If the cursor is in the text, not at the end
567 |
568 | if (inp == '\0') {
569 | // Backspace
570 |
571 | // Move all of in after pos one space back
572 | for (int i = pos; i <= len; i++) {
573 | in[i] = in[i + 1];
574 | }
575 |
576 | sweepline(inputwin, 1, 22 + len);
577 | }
578 | else {
579 | // Everything except backspace
580 |
581 | // Move all of in after pos one space forward to make room for the new input
582 | len++;
583 | for (int i = len; i > pos; i--) {
584 | in[i] = in[i - 1];
585 | }
586 | // Append char to in array
587 | in[pos++] = inp;
588 |
589 | }
590 | }
591 | }
592 | // This saves having to increment pos everytime len is incremented when youre not browsing
593 | if (!browsing) { pos = len; }
594 |
595 | // Clear input (only necessary because of the history feature)
596 | sweepline(inputwin, 1, 22);
597 |
598 | // Finaly print input
599 | if (use_interface)
600 | mvwprintw_colors(inputwin, 1, 22, COLOR_PAIR_DEFAULT, "%s", in);
601 | else
602 | mvwprintw(inputwin, 1, 22, "%s", in);
603 |
604 | wmove(inputwin, 1, 22 + pos); // Move the cursor
605 |
606 | wrefresh(inputwin);
607 |
608 | }
609 |
610 | if (in[0] != '\0' && (searchHistory.size == 0 || strcmp(in, searchHistory.records[searchHistory.size - 1]))) {
611 | add_to_history(&searchHistory, in);
612 | }
613 |
614 | }
615 |
616 |
617 | void exit_pcalc(int code) {
618 |
619 | free_history(&history);
620 | free_history(&searchHistory);
621 | free_numberstack(numbers);
622 |
623 | endwin();
624 |
625 | switch (code) {
626 |
627 | case 0: break;
628 | case MEM_FAIL: fprintf(stderr, "OUT OF MEMORY\n"); break;
629 |
630 |
631 | }
632 |
633 | exit(code);
634 | }
635 |
636 | static void exit_pcalc_success() {
637 |
638 | exit_pcalc(0);
639 | }
640 |
--------------------------------------------------------------------------------
/src/numberstack.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "numberstack.h"
4 | #include "xmalloc.h"
5 | #include "global.h"
6 |
7 |
8 | numberstack* numbers;
9 |
10 |
11 | // Allocate and set up numberstack
12 | numberstack * create_numberstack(int max_size) {
13 |
14 | numberstack* s;
15 | s = xmalloc(sizeof(numberstack));
16 | void* allocated[] = { s };
17 | s->elements = xmalloc_with_ressources(max_size * sizeof(*s->elements), allocated, 1);
18 | s->size = 0;
19 | s->max_size = max_size;
20 | return s;
21 | }
22 |
23 | static numberstack * resize_numberstack(numberstack* s) {
24 |
25 | s->max_size *= 2;
26 | void* allocated[] = { s };
27 | s->elements = xrealloc_with_ressources(s->elements, s->max_size * sizeof(*s->elements), allocated, 1);
28 | return s;
29 |
30 | }
31 |
32 | // Pop element from the top of the stack (return and remove element)
33 | uint64_t * pop_numberstack(numberstack* s) {
34 |
35 | if (s->size == 0)
36 | return NULL;
37 |
38 | return &s->elements[--s->size];
39 | }
40 |
41 | // Return the element at the top of the stack without removing it
42 | uint64_t * top_numberstack(numberstack* s) {
43 |
44 | if (s->size == 0)
45 | return NULL;
46 |
47 | return &s->elements[s->size-1];
48 | }
49 |
50 | // Push number to the top of the stack
51 | void push_numberstack(numberstack* s, uint64_t value) {
52 |
53 | if (s->size == s->max_size)
54 | resize_numberstack(s);
55 |
56 | s->elements[s->size++] = value;
57 | }
58 |
59 | // Clear the stack
60 | void clear_numberstack(numberstack* s) {
61 |
62 | s->size = 0;
63 | }
64 |
65 | void free_numberstack(numberstack *s) {
66 |
67 | xfree(s->elements);
68 | xfree(s);
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/src/operators.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #include "operators.h"
5 |
6 | uint64_t globalmask = DEFAULT_MASK;
7 | int globalmasksize = DEFAULT_MASK_SIZE;
8 |
9 | operation *current_op = NULL;
10 |
11 | static uint64_t add(uint64_t, uint64_t);
12 | static uint64_t subtract(uint64_t, uint64_t);
13 | static uint64_t multiply(uint64_t, uint64_t);
14 | static uint64_t divide(uint64_t, uint64_t);
15 | static uint64_t and(uint64_t, uint64_t);
16 | static uint64_t or(uint64_t, uint64_t);
17 | static uint64_t nor(uint64_t, uint64_t);
18 | static uint64_t xor(uint64_t, uint64_t);
19 | static uint64_t shl(uint64_t, uint64_t);
20 | static uint64_t rol(uint64_t, uint64_t);
21 | static uint64_t modulus(uint64_t, uint64_t);
22 | static uint64_t not(uint64_t, uint64_t);
23 | static uint64_t twos_complement(uint64_t, uint64_t);
24 | static uint64_t swap_endianness(uint64_t, uint64_t);
25 |
26 | static operation operations[16] = {
27 | {ADD_SYMBOL, 2, add},
28 | {SUB_SYMBOL, 2, subtract},
29 | {MUL_SYMBOL, 2, multiply},
30 | {DIV_SYMBOL, 2, divide},
31 | {AND_SYMBOL, 2, and},
32 | {OR_SYMBOL, 2, or},
33 | {NOR_SYMBOL, 2, nor},
34 | {XOR_SYMBOL, 2, xor},
35 | {SHL_SYMBOL, 2, shl},
36 | {SHR_SYMBOL, 2, shr},
37 | {ROL_SYMBOL, 2, rol},
38 | {ROR_SYMBOL, 2, ror},
39 | {MOD_SYMBOL, 2, modulus},
40 | {NOT_SYMBOL, 1, not},
41 | {TWOSCOMPLEMENT_SYMBOL, 1, twos_complement},
42 | {SWAPENDIANNESS_SYMBOL, 1, swap_endianness}
43 | };
44 |
45 | operation* getopcode(char c) {
46 |
47 | for (unsigned long i=0; i < sizeof(operations); i++)
48 | if (operations[i].character == c)
49 | return &operations[i];
50 |
51 | return NULL;
52 | }
53 |
54 |
55 | static uint64_t add(uint64_t a, uint64_t b) {
56 |
57 | return a + b;
58 | }
59 |
60 | // remember op1 = first popped ( right operand ), op2 = second popped ( left operand )
61 | static uint64_t subtract(uint64_t a, uint64_t b) {
62 |
63 | return b - a;
64 | }
65 | static uint64_t multiply(uint64_t a, uint64_t b) {
66 |
67 | return a * b;
68 | }
69 |
70 | static uint64_t divide(uint64_t a, uint64_t b) {
71 |
72 | //TODO not divisible by 0
73 | if(!a)
74 | return 0;
75 |
76 | return b / a;
77 | }
78 |
79 | static uint64_t and(uint64_t a, uint64_t b) {
80 |
81 | return a & b;
82 | }
83 |
84 | static uint64_t or(uint64_t a, uint64_t b) {
85 |
86 | return a | b;
87 | }
88 |
89 | static uint64_t nor(uint64_t a, uint64_t b) {
90 |
91 | return ~(a | b);
92 | }
93 |
94 | static uint64_t xor(uint64_t a, uint64_t b) {
95 |
96 | return a ^ b;
97 | }
98 | static uint64_t shl(uint64_t a, uint64_t b) {
99 |
100 | // Shift longer than type length is undefined behaviour
101 | return b << a;
102 | }
103 |
104 | uint64_t shr(uint64_t a, uint64_t b) {
105 |
106 | // Shift longer than 64 bits is undefined behaviour
107 | // don't include shift in tests or //TODO: define behaviour for this calculator
108 | return ((uint64_t) b >> a);
109 | }
110 |
111 | static uint64_t rol(uint64_t a, uint64_t b) {
112 |
113 | // prevent shift by 64 bits because a shift longer than type length is undefined behaviour
114 | return b << a | ( globalmasksize - a < 64 ? shr(globalmasksize - a, b) : 0 );
115 | }
116 |
117 | uint64_t ror(uint64_t a, uint64_t b) {
118 |
119 | // prevent shift by 64 bits because a shift longer than type length is undefined behaviour
120 | return shr(a, b) | (globalmasksize - a < 64 ? b << (globalmasksize - a) : 0);
121 | }
122 |
123 | static uint64_t modulus(uint64_t a, uint64_t b) {
124 |
125 | //TODO not divisible by 0
126 | if(!a)
127 | return 0;
128 |
129 | return b % a;
130 | }
131 |
132 | static uint64_t not(uint64_t a, uint64_t UNUSED(b)) {
133 |
134 | return ~a;
135 | }
136 |
137 | static uint64_t twos_complement(uint64_t a, uint64_t UNUSED(b)) {
138 |
139 | return -a;
140 | }
141 |
142 | static uint64_t swap_endianness(uint64_t a, uint64_t UNUSED(b)) {
143 |
144 | uint64_t out = 0;
145 | // shift the leftmost bits to the right
146 | for (int i = 0; i < globalmasksize / 16; i++) {
147 | // create a bitmask and apply it to a and shift the selected byte to its new position
148 | out |= (a & (0xffull << (globalmasksize-8 - i*8)) ) >> (globalmasksize-8 - i*16);
149 | }
150 | // shift the rightmost bits left (and leave the miidle bit in place in the case of an odd number of bytes)
151 | for (int i = 0; i < globalmasksize / 16 + 1; i++) {
152 | out |= (a & (0xffull << (((globalmasksize/2 - 1) & -8) - i*8)) ) << (((globalmasksize/8 & 1) ? 0 : 8) + i*16);
153 | }
154 | return out;
155 | }
156 |
--------------------------------------------------------------------------------
/src/parser.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 |
7 | #include "parser.h"
8 | #include "xmalloc.h"
9 |
10 | // Static functions
11 |
12 | static exprtree parse_expr(parser_t);
13 | static exprtree parse_or_expr(parser_t);
14 | static exprtree parse_xor_expr(parser_t);
15 | static exprtree parse_and_expr(parser_t);
16 | static exprtree parse_shift_expr(parser_t);
17 | static exprtree parse_add_expr(parser_t);
18 | static exprtree parse_mult_expr(parser_t);
19 | static exprtree parse_prefix_expr(parser_t);
20 | static exprtree parse_atom_expr(parser_t);
21 | static exprtree parse_number(parser_t);
22 |
23 | static exprtree parse_stdop_expr(parser_t, char*, exprtree (*) (parser_t));
24 |
25 | static exprtree create_exprtree(int, void*, exprtree, exprtree);
26 |
27 | int total_trees_created = 0;
28 | int total_trees_freed = 0;
29 | int total_parsers_created = 0;
30 | int total_parsers_freed = 0;
31 | int total_tokens_created = 0;
32 | int total_tokens_freed = 0;
33 |
34 |
35 | // For a simpler version of this parser check github.com/alt-romes/calculator-c-parser
36 |
37 | /**
38 | * @brief Sanitize input to only allowed characters
39 | *
40 | * Mallocs a new string with only allowed characters
41 | */
42 | char* sanitize(const char* in) {
43 |
44 | char* output = xmalloc(sizeof(char) * MAX_CHARS);
45 |
46 | int in_len = strlen(in);
47 | int token_pos = 0;
48 | for (int i = 0; i < in_len; i++)
49 | if (strchr(VALID_TOKENS, in[i]))
50 | output[token_pos++] = in[i];
51 |
52 | output[token_pos] = '\0';
53 |
54 | total_tokens_created++;
55 | return output;
56 | }
57 |
58 |
59 | /**
60 | * @brief Parse sanitized input into an expression tree
61 | *
62 | * Entry point to the parser. Frees input after parsing.
63 | */
64 | exprtree parse(char* input) {
65 |
66 | // TODO: How to stop with errors?
67 |
68 | // attention: allocate size for *struct parser_t*, because *parser_t* is type defined as a pointer to *struct parser_t*
69 | parser_t parser = xmalloc(sizeof(struct parser_t));
70 | total_parsers_created++;
71 |
72 | assert(input != NULL);
73 | parser->tokens = input;
74 |
75 | int ntokens = strlen(input);
76 | assert(ntokens > 0);
77 | parser->ntokens = ntokens;
78 |
79 | parser->pos = 0;
80 |
81 | exprtree expression = parse_expr(parser);
82 |
83 | free(parser->tokens);
84 | free(parser);
85 | total_parsers_freed++;
86 |
87 | total_tokens_freed++;
88 | return expression;
89 | }
90 |
91 | /**
92 | * @brief Calculate a numeric value from an expression tree
93 | */
94 | uint64_t calculate(exprtree expr) {
95 |
96 | // expr shouldn't be null if being calculated.
97 | assert(expr != NULL);
98 | // all expressions have 2 operands because 1 operand operators are taken care of immediately
99 |
100 | if (expr->type == OP_TYPE) {
101 |
102 | uint64_t left_value = calculate(expr->left);
103 |
104 | uint64_t right_value = calculate(expr->right);
105 |
106 | // Execute takes the operands switched because the stack inverts the order of the numbers
107 | uint64_t value = expr->op->execute(right_value, left_value);
108 |
109 | return value & globalmask;
110 |
111 | }
112 | else {
113 |
114 | // Expression is a leaf (is a number) - so return the number directly
115 |
116 | return *(expr->value) & globalmask;
117 | }
118 |
119 | }
120 |
121 | /**
122 | * @brief Free an expression tree and all its children
123 | */
124 | void free_exprtree(exprtree expr) {
125 |
126 | if (expr) {
127 |
128 | if (expr->left)
129 | free_exprtree(expr->left);
130 | if (expr->right)
131 | free_exprtree(expr->right);
132 |
133 |
134 | if (expr->type != OP_TYPE)
135 | free(expr->value);
136 |
137 | free(expr);
138 |
139 | total_trees_freed++;
140 |
141 | }
142 |
143 | }
144 |
145 | /**
146 | * @brief Parse sub-string in parser into expression tree
147 | *
148 | * Works by recursively calling different parse functions until all tokens gets consumed.
149 | * Sarts with lowest precedence.
150 | * Inner recursive calls, which get executed first, are higher precedence.
151 | */
152 | static exprtree parse_expr(parser_t parser) {
153 |
154 | // Grammar rule: expression := or_exp
155 |
156 | return parse_or_expr(parser);
157 | }
158 |
159 | static exprtree parse_or_expr(parser_t parser) {
160 |
161 | // Grammar rule: or_exp := xor_exp ( (| | $) xor_exp )*
162 |
163 | char ops[] = {OR_SYMBOL, NOR_SYMBOL, '\0'};
164 |
165 | return parse_stdop_expr(parser, ops, parse_xor_expr);
166 |
167 | }
168 |
169 | static exprtree parse_xor_expr(parser_t parser) {
170 |
171 | // Grammar rule: xor_exp := and_exp (^ and_exp)*
172 |
173 | char ops[] = {XOR_SYMBOL, '\0'};
174 |
175 | return parse_stdop_expr(parser, ops, parse_and_expr);
176 |
177 | }
178 |
179 | static exprtree parse_and_expr(parser_t parser) {
180 |
181 | // Grammar rule: and_exp := shift_exp (& shift_exp)*
182 |
183 | char ops[] = {AND_SYMBOL, '\0'};
184 |
185 | return parse_stdop_expr(parser, ops, parse_shift_expr);
186 |
187 | }
188 |
189 | static exprtree parse_shift_expr(parser_t parser) {
190 |
191 | // Grammar rule: shift_exp := add_exp ((<< | >> | ror | rol) add_exp)*
192 |
193 | char ops[] = {SHR_SYMBOL, SHL_SYMBOL, ROR_SYMBOL, ROL_SYMBOL, '\0'};
194 |
195 | return parse_stdop_expr(parser, ops, parse_add_expr);
196 |
197 | }
198 |
199 | static exprtree parse_add_expr(parser_t parser) {
200 |
201 | // Grammar rule: add_exp := mult_exp ((+ | -) mult_exp)*
202 |
203 | char ops[] = {ADD_SYMBOL, SUB_SYMBOL, '\0'};
204 |
205 | return parse_stdop_expr(parser, ops, parse_mult_expr);
206 |
207 | }
208 |
209 | static exprtree parse_mult_expr(parser_t parser) {
210 |
211 | // Grammar rule: mult_exp := not_exp ((* | / | %) not_exp)*
212 |
213 | char ops[] = {MUL_SYMBOL, DIV_SYMBOL, MOD_SYMBOL, '\0'};
214 |
215 | return parse_stdop_expr(parser, ops, parse_prefix_expr);
216 |
217 | }
218 |
219 | static exprtree parse_prefix_expr(parser_t parser) {
220 |
221 | // Grammar rule: prefix_exp := (~ | + | - | @)? atom_exp
222 |
223 | // TODO: Display input invalid instead of using a zero-val expression
224 | if (!(parser->pos < parser->ntokens)) {
225 |
226 | uint64_t zerov = 0;
227 | return create_exprtree(DEC_TYPE, &zerov, NULL, NULL);
228 | }
229 |
230 |
231 | char prefixes[] = {ADD_SYMBOL, SUB_SYMBOL, NOT_SYMBOL, TWOSCOMPLEMENT_SYMBOL, SWAPENDIANNESS_SYMBOL, '\0'};
232 |
233 | // If we've exceeded the number of tokens we should detect an error
234 | assert(parser->pos < parser->ntokens);
235 |
236 | char prefix = 0;
237 | if (strchr(prefixes, parser->tokens[parser->pos])) {
238 |
239 | // Find + or - before the number (to make numbers positive or negative)
240 |
241 | // When the symbol found is +, there's no need to do anything
242 |
243 | prefix = parser->tokens[parser->pos];
244 |
245 | parser->pos++; // Consume token
246 | }
247 |
248 | exprtree expr = parse_atom_expr(parser);
249 |
250 | if (prefix == 0 || prefix == ADD_SYMBOL) // Do nothing to expression
251 | return expr;
252 | else {
253 |
254 | // Prefix is either SUB_SYMBOL, NOT_SYMBOL, TWOSCOMPLEMENT_SYMBOL or SWAPENDIANNESS_SYMBOL
255 |
256 | // We get the operation to use in the expression
257 | operation* op = getopcode(prefix);
258 |
259 | // SUB sets the symmetric of number with the expression (0 - expression), so we create a
260 | // subtraction expression with 0 as the left tree
261 |
262 | // Bitwise NOT of a number - only one parameter is used:
263 | // And because the order is changed in execute(), put a number in the right expr instead of the left one
264 | // apply the NOT operation to the number on the right branch. The other branch doesn't matter so we set it as the same as SUB
265 |
266 | // Two's complement serves the same logic - since it only uses one parameter we can set the other as anything
267 |
268 | // So we create an expression with 0 on the left, and the correct op, and it works
269 | uint64_t zero_val = 0;
270 | exprtree zero_val_expr = create_exprtree(DEC_TYPE, &zero_val, NULL, NULL);
271 | return create_exprtree(OP_TYPE, op, zero_val_expr, expr);
272 | }
273 |
274 | }
275 |
276 | /**
277 | * @brief Tries to parse next expression which may consist of parentheses or a number
278 | */
279 | static exprtree parse_atom_expr(parser_t parser) {
280 |
281 | // Grammar rule: atom_expr := number | left_parenthesis expression right_parenthesis
282 |
283 | // TODO: An error should be displayed here instead of return a zero value expression.
284 | // This assertion fails if the only token is a prefix, the prefix is read, and this function is called without enough tokens
285 | // to be parsed. There are possibly more cases
286 | if (!(parser->pos < parser->ntokens)) {
287 |
288 | uint64_t zerov = 0;
289 | return create_exprtree(DEC_TYPE, &zerov, NULL, NULL);
290 | }
291 |
292 | // If we've exceeded the number of tokens we should detect an error
293 | assert(parser->pos < parser->ntokens);
294 |
295 | exprtree expr;
296 |
297 | if (parser->tokens[parser->pos] == LPAR_SYMBOL) {
298 | // If the atomic expression starts with parenthesis
299 |
300 | parser->pos++; // Consume left parenthesis
301 |
302 | expr = parse_expr(parser);
303 |
304 | // If we've exceeded the number of tokens we should detect an error
305 | // This assertion is triggered if an expression without right parenthesis is the input
306 | // It should be handled as an error below
307 | /* assert(parser->pos < parser->ntokens); */
308 |
309 | if (parser->tokens[parser->pos] == RPAR_SYMBOL)
310 | parser->pos++; // Consume right parenthesis
311 | else {
312 |
313 | // For now, everything to the right of an unclosed left parenthesis will be equivalent to 0
314 | uint64_t zerov = 0;
315 | return create_exprtree(DEC_TYPE, &zerov, NULL, NULL);
316 |
317 | // TODO: Find a way to do error handling and displaying, possibly give one more type to exprtree type = ERR_TYPE and have in the union a char* for the error message
318 | /* fprintf(stderr, "Invalid expression!!!\n"); */
319 | }
320 |
321 | }
322 | else {
323 | // If it doesn't start with parenthesis then it's a normal number
324 |
325 | expr = parse_number(parser);
326 | }
327 |
328 | return expr;
329 |
330 | }
331 |
332 | /**
333 | * @brief Tries to parse next token as a number
334 | */
335 | static exprtree parse_number(parser_t parser) {
336 |
337 | // Grammar rule: number: ( (0-9)+ | 0?x(0-9a-f)+ | 0?b(0-1)+ )
338 |
339 | // If we've exceeded the number of tokens we should detect an error
340 | assert(parser->pos < parser->ntokens);
341 |
342 | int numbertype = DEC_TYPE;
343 | if (parser->pos+1 < parser->ntokens) {
344 | switch (parser->tokens[parser->pos]) {
345 | case '0':
346 | // check second character
347 | switch(parser->tokens[parser->pos+1]) {
348 | case 'b': // 0b0101
349 | numbertype = BIN_TYPE;
350 | parser->pos += 2;
351 | break;
352 | case 'x': // 0xffff
353 | numbertype = HEX_TYPE;
354 | parser->pos += 2;
355 | break;
356 | default: // number is decimal
357 | break;
358 | }
359 | break;
360 | case 'x': // xffff
361 | numbertype = HEX_TYPE;
362 | parser->pos += 1;
363 | break;
364 | case 'b': // b0101
365 | numbertype = BIN_TYPE;
366 | parser->pos += 1;
367 | break;
368 | }
369 |
370 | }
371 |
372 | char numberfound[MAX_CHARS + 1];
373 | int numberlen = 0;
374 |
375 | while ( parser->pos < parser->ntokens &&
376 | ((numbertype == DEC_TYPE && strchr(VALID_DEC_SYMBOLS, parser->tokens[parser->pos]))
377 | || (numbertype == HEX_TYPE && strchr(VALID_HEX_SYMBOLS, parser->tokens[parser->pos]))
378 | || (numbertype == BIN_TYPE && strchr(VALID_BIN_SYMBOLS, parser->tokens[parser->pos]))) ) {
379 |
380 | numberfound[numberlen++] = parser->tokens[parser->pos];
381 |
382 | parser->pos++; // Consume 1 digit (1 token)
383 |
384 | }
385 | numberfound[numberlen] = '\0';
386 |
387 | // If no number was found, return for now a zero value expression
388 | //
389 | // TODO: return the error expression instead
390 | // this happens when the input is valid tokens like abc, but then the number doesn't start with 0x,
391 | // and possibly in other situations
392 | if (numberlen == 0) {
393 |
394 | uint64_t zerov = 0;
395 | return create_exprtree(DEC_TYPE, &zerov, NULL, NULL);
396 | }
397 |
398 | // Else, create the expression from the found number
399 | int numberbase = 0;
400 | switch (numbertype) {
401 | case DEC_TYPE:
402 | numberbase = 10;
403 | break;
404 | case HEX_TYPE:
405 | numberbase = 16;
406 | break;
407 | case BIN_TYPE:
408 | numberbase = 2;
409 | break;
410 | }
411 |
412 | uint64_t value = strtoull(numberfound, NULL, numberbase);
413 |
414 | exprtree number_expr = create_exprtree(numbertype, &value, NULL, NULL);
415 |
416 | return number_expr;
417 | }
418 |
419 | /**
420 | * @brief Tries to parse next binary expression with any of chars in ops as operator
421 | *
422 | * First tries to parse left side of operation with parse_inner_expr.
423 | * This means that parse_inner_expr gets a higher priority in operator precedence.
424 | * Then looks for any of the chars in ops, if found, tries to parse right side of operation with parse_inner_expr.
425 | * If the operator was not found, this means that parsing of the current sub-string is done.
426 | */
427 | static exprtree parse_stdop_expr(parser_t parser, char* ops, exprtree (*parse_inner_expr) (parser_t)) {
428 |
429 | // TODO: We don't want to do this - when the input is badly formatted an error should be displayed.
430 | // This is a temporary fix that returns the expression immediately as zero.
431 | if (!(parser->pos < parser->ntokens)) {
432 |
433 | uint64_t zerov = 0;
434 | return create_exprtree(DEC_TYPE, &zerov, NULL, NULL);
435 | }
436 |
437 | // When the position gets here it should be smaller than the ntokens, or maybe only inner_expr should worry about it?
438 | assert(parser->pos < parser->ntokens);
439 |
440 | exprtree expr = parse_inner_expr(parser);
441 |
442 | while (parser->pos < parser->ntokens && strchr(ops, parser->tokens[parser->pos])) {
443 |
444 | operation* op = getopcode(parser->tokens[parser->pos]);
445 |
446 | parser->pos++; // Consume token
447 |
448 | exprtree right_expr = parse_inner_expr(parser);
449 |
450 | expr = create_exprtree(OP_TYPE, op, expr, right_expr);
451 | }
452 |
453 | return expr;
454 |
455 | }
456 |
457 | /**
458 | * @brief Create a new expression tree node.
459 | */
460 | static exprtree create_exprtree(int type, void* content, exprtree left, exprtree right) {
461 |
462 | // attention: allocate size for *struct exprtree*, because *exprtree* is type defined as a pointer to *struct exprtree*
463 | exprtree expr = xmalloc(sizeof(struct exprtree));
464 |
465 | expr->type = type;
466 |
467 | if (type == OP_TYPE)
468 | expr->op = getopcode(*((char*) content));
469 |
470 | else {
471 |
472 | void* allocated[] = { expr };
473 | expr->value = xmalloc_with_ressources(sizeof(*expr->value), allocated, 1);
474 | *(expr->value) = *((uint64_t*) content);
475 | }
476 |
477 | expr->left = left;
478 | expr->right = right;
479 |
480 | total_trees_created++;
481 | return expr;
482 |
483 | }
484 |
485 |
--------------------------------------------------------------------------------
/src/xmalloc.c:
--------------------------------------------------------------------------------
1 | #include "xmalloc.h"
2 |
3 | #include "global.h"
4 |
5 |
6 | /**
7 | * behaves the same as malloc but kills the program if malloc fails
8 | * @param bytes size of allocation
9 | */
10 | void *xmalloc(size_t bytes) {
11 | return xmalloc_with_ressources(bytes, NULL, 0);
12 | }
13 |
14 | /**
15 | * behaves the same as malloc but kills the program if malloc fails
16 | * @param bytes size of allocation
17 | * @param pntrs is the list of pointers to free
18 | * @param npntrs is the amount of pointers to free = size of array
19 | */
20 | void* xmalloc_with_ressources(size_t bytes, void** ressources, size_t nres) {
21 | void* temp = malloc(bytes);
22 | if (temp == NULL) {
23 | xfreen(ressources, nres);
24 | exit_pcalc(MEM_FAIL);
25 | }
26 | return temp;
27 | }
28 |
29 | /**
30 | * behaves the same as calloc but kills the program if malloc fails
31 | * @param nelem number of elements
32 | * @param bytes byte that is set
33 | */
34 | void* xcalloc(size_t nelem, size_t bytes) {
35 | return xcalloc_with_ressources(nelem, bytes, NULL, 0);
36 | }
37 |
38 |
39 | /**
40 | * behaves the same as calloc but kills the program if malloc fails
41 | * @param nelem number of elements
42 | * @param bytes byte that is set
43 | * @param pntrs is the list of pointers to free
44 | * @param npntrs is the amount of pointers to free = size of array
45 | */
46 | void* xcalloc_with_ressources(size_t nelem, size_t bytes, void** ressources, size_t nres) {
47 | void *temp = calloc(nelem, bytes);
48 | if (temp == NULL) {
49 | xfreen(ressources, nres);
50 | exit_pcalc(MEM_FAIL);
51 | }
52 | return temp;
53 | }
54 |
55 | /**
56 | * behaves the same as xrealloc but kills the program if malloc fails
57 | * @param pntr pointer to reallocate
58 | * @param bytes new size
59 | */
60 | void* xrealloc(void *pntr, size_t bytes) {
61 | return xrealloc_with_ressources(pntr, bytes, NULL, 0);
62 | }
63 |
64 |
65 | /**
66 | * behaves the same as realloc but kills the program if malloc fails
67 | * @param pntr pointer to reallocate
68 | * @param bytes new size
69 | * @param pntrs is the list of pointers to free
70 | * @param npntrs is the amount of pointers to free = size of array
71 | */
72 | void* xrealloc_with_ressources(void* pntr, size_t bytes, void** ressources, size_t nres) {
73 | void* temp = pntr ? realloc(pntr, bytes) : xmalloc(bytes);
74 | if (temp == NULL) {
75 | xfreen(ressources, nres);
76 | exit_pcalc(MEM_FAIL);
77 | }
78 | return (temp);
79 | }
80 |
81 | /**
82 | * Frees npntrs elements
83 | * @param pntrs is the list of pointers to free
84 | * @param npntrs is the amount of pointers to free = size of array
85 | */
86 | void xfreen(void** pntrs, size_t npntrs) {
87 | for (size_t i = 0; i < npntrs; ++i) {
88 | xfree(pntrs[i]);
89 | }
90 | }
91 |
92 | /**
93 | * behaves the same as free
94 | * @param pntr the pointer to be freed
95 | */
96 | void xfree(void* pntr) {
97 | free(pntr);
98 | }
99 |
--------------------------------------------------------------------------------
/tests/corner-cases.correct:
--------------------------------------------------------------------------------
1 | Decimal: 0, Hex: 0x0, Operation:
2 | Decimal: 9223372036854775807, Hex: 0x7fffffffffffffff, Operation:
3 | Decimal: -9223372036854775808, Hex: 0x8000000000000000, Operation:
4 | Decimal: -1, Hex: 0xffffffffffffffff, Operation:
5 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
6 |
--------------------------------------------------------------------------------
/tests/corner-cases.test:
--------------------------------------------------------------------------------
1 | 0x7fffffffffffffff
2 | 0x8000000000000000
3 | 0xffffffffffffffff
4 | 0xfffffffffffffffe
5 | exit
6 |
--------------------------------------------------------------------------------
/tests/expressions.correct:
--------------------------------------------------------------------------------
1 | Decimal: 0, Hex: 0x0, Operation:
2 | Decimal: 2, Hex: 0x2, Operation:
3 | Decimal: 6, Hex: 0x6, Operation:
4 | Decimal: 10, Hex: 0xa, Operation:
5 | Decimal: 15, Hex: 0xf, Operation:
6 | Decimal: -1, Hex: 0xffffffffffffffff, Operation:
7 | Decimal: -4, Hex: 0xfffffffffffffffc, Operation:
8 | Decimal: -8, Hex: 0xfffffffffffffff8, Operation:
9 | Decimal: -13, Hex: 0xfffffffffffffff3, Operation:
10 | Decimal: 2, Hex: 0x2, Operation:
11 | Decimal: 6, Hex: 0x6, Operation:
12 | Decimal: 24, Hex: 0x18, Operation:
13 | Decimal: 120, Hex: 0x78, Operation:
14 | Decimal: 0, Hex: 0x0, Operation:
15 | Decimal: 0, Hex: 0x0, Operation:
16 | Decimal: 0, Hex: 0x0, Operation:
17 | Decimal: 0, Hex: 0x0, Operation:
18 | Decimal: 0, Hex: 0x0, Operation:
19 | Decimal: 0, Hex: 0x0, Operation:
20 | Decimal: 0, Hex: 0x0, Operation:
21 | Decimal: 1, Hex: 0x1, Operation:
22 | Decimal: 1, Hex: 0x1, Operation:
23 | Decimal: 1, Hex: 0x1, Operation:
24 | Decimal: 1, Hex: 0x1, Operation:
25 | Decimal: 1, Hex: 0x1, Operation:
26 | Decimal: 0, Hex: 0x0, Operation:
27 | Decimal: 0, Hex: 0x0, Operation:
28 | Decimal: 0, Hex: 0x0, Operation:
29 | Decimal: 0, Hex: 0x0, Operation:
30 | Decimal: 3, Hex: 0x3, Operation:
31 | Decimal: 3, Hex: 0x3, Operation:
32 | Decimal: 7, Hex: 0x7, Operation:
33 | Decimal: 7, Hex: 0x7, Operation:
34 | Decimal: -4, Hex: 0xfffffffffffffffc, Operation:
35 | Decimal: 0, Hex: 0x0, Operation:
36 | Decimal: -5, Hex: 0xfffffffffffffffb, Operation:
37 | Decimal: 0, Hex: 0x0, Operation:
38 | Decimal: 3, Hex: 0x3, Operation:
39 | Decimal: 0, Hex: 0x0, Operation:
40 | Decimal: 4, Hex: 0x4, Operation:
41 | Decimal: 1, Hex: 0x1, Operation:
42 | Decimal: 4, Hex: 0x4, Operation:
43 | Decimal: 32, Hex: 0x20, Operation:
44 | Decimal: 512, Hex: 0x200, Operation:
45 | Decimal: 16384, Hex: 0x4000, Operation:
46 | Decimal: 0, Hex: 0x0, Operation:
47 | Decimal: 0, Hex: 0x0, Operation:
48 | Decimal: 0, Hex: 0x0, Operation:
49 | Decimal: 0, Hex: 0x0, Operation:
50 | Decimal: 4, Hex: 0x4, Operation:
51 | Decimal: 32, Hex: 0x20, Operation:
52 | Decimal: 512, Hex: 0x200, Operation:
53 | Decimal: 16384, Hex: 0x4000, Operation:
54 | Decimal: 4611686018427387904, Hex: 0x4000000000000000, Operation:
55 | Decimal: 576460752303423488, Hex: 0x800000000000000, Operation:
56 | Decimal: 36028797018963968, Hex: 0x80000000000000, Operation:
57 | Decimal: 1125899906842624, Hex: 0x4000000000000, Operation:
58 | Decimal: 0, Hex: 0x0, Operation:
59 | Decimal: -8613303245920329199, Hex: 0x8877665544332211, Operation:
60 | Decimal: 33608038631023121, Hex: 0x77665544332211, Operation:
61 | Decimal: 33608038631023121, Hex: 0x77665544332211, Operation:
62 | Decimal: 112516402455057, Hex: 0x665544332211, Operation:
63 | Decimal: 112516402455057, Hex: 0x665544332211, Operation:
64 | Decimal: 366216421905, Hex: 0x5544332211, Operation:
65 | Decimal: 366216421905, Hex: 0x5544332211, Operation:
66 | Decimal: 1144201745, Hex: 0x44332211, Operation:
67 | Decimal: 1144201745, Hex: 0x44332211, Operation:
68 | Decimal: 3351057, Hex: 0x332211, Operation:
69 | Decimal: 3351057, Hex: 0x332211, Operation:
70 | Decimal: 8721, Hex: 0x2211, Operation:
71 | Decimal: 8721, Hex: 0x2211, Operation:
72 | Decimal: 17459, Hex: 0x4433, Operation:
73 | Decimal: 51, Hex: 0x33, Operation:
74 | Decimal: 17, Hex: 0x11, Operation:
75 |
--------------------------------------------------------------------------------
/tests/expressions.test:
--------------------------------------------------------------------------------
1 | 1+1
2 | 1+2+3
3 | 1+2+3+4
4 | 1+2+3+4+5
5 | 1-2
6 | 1-2-3
7 | 1-2-3-4
8 | 1-2-3-4-5
9 | 1*2
10 | 1*2*3
11 | 1*2*3*4
12 | 1*2*3*4*5
13 | 1/2
14 | 1/2/3
15 | 1/2/3/4
16 | 1/2/3/4/5
17 | 5/4/3/2/1
18 | 5/4/3/2
19 | 5/4/3
20 | 5/4
21 | 1%2
22 | 1%2%3
23 | 1%2%3%4
24 | 1%2%3%4%5
25 | 1&2
26 | 1&2&3
27 | 1&2&3&4
28 | 1&2&3&4&5
29 | 1|2
30 | 1|2|3
31 | 1|2|3|4
32 | 1|2|3|4|5
33 | 1$2
34 | 1$2$3
35 | 1$2$3$4
36 | 1$2$3$4$5
37 | 1^2
38 | 1^2^3
39 | 1^2^3^4
40 | 1^2^3^4^5
41 | 1<2
42 | 1<2<3
43 | 1<2<3<4
44 | 1<2<3<4<5
45 | 1>2
46 | 1>2>3
47 | 1>2>3>4
48 | 1>2>3>4>5
49 | 1:2
50 | 1:2:3
51 | 1:2:3:4
52 | 1:2:3:4:5
53 | 1;2
54 | 1;2;3
55 | 1;2;3;4
56 | 1;2;3;4;5
57 |
58 | @0x1122334455667788
59 | 56bit
60 | @0x11223344556677
61 | 48bit
62 | @0x112233445566
63 | 40bit
64 | @0x1122334455
65 | 32bit
66 | @0x11223344
67 | 24bit
68 | @0x112233
69 | 16bit
70 | @0x1122
71 | @0x11223344
72 | 8bit
73 | @0x11
74 | exit
75 |
--------------------------------------------------------------------------------
/tests/how-to-test.md:
--------------------------------------------------------------------------------
1 | ## Testing
2 |
3 | The folder `tests` has two files for each test. `file.test` and `file.correct`
4 |
5 | The `.test` file is the input passed into `pcalc -n`, and the `.correct` file is the expected output
6 |
7 | To test one of these files run:
8 | ```
9 | $ diff -b tests/number-bases.correct <(cat tests/number-bases.test | ./pcalc -n)
10 | ```
11 |
12 | If something is printed out to the console then the actual output and the expected output differ, and changes should be made until all tests pass.
13 |
14 | To test all files at the same time run:
15 | ```
16 | $ ./run-tests.sh
17 | ```
18 |
19 | It would be better to run `./run-tests.sh` multiple times because i.e. I currently have a bug that crashes the code with segfault only half of the times
20 |
21 | ### Test file
22 |
23 | A test file contains sequences of expressions to be run in the calculator and must end with "quit" or "exit"
24 |
25 | When creating a new test file, add the name to the array of tests in `run-tests.sh`
26 |
27 | ### Writing a test
28 |
29 | I've found that the best way to write a test is by testing multiple operations in the calculator while checking it's result, and save the operations done. If everything you observe is correct, you run the test and save the output as the correction.
30 |
31 | First: Test input while saving it
32 | ```
33 | $ tee -a tests/name-of-test.test | ./pcalc -n
34 | ```
35 |
36 | If all results from the input inserted are correct, save the output as the correction
37 | ```
38 | $ cat tests/name-of-test.test | ./pcalc -n > name-of-test.correct
39 | ```
40 |
--------------------------------------------------------------------------------
/tests/input-formats.correct:
--------------------------------------------------------------------------------
1 | Decimal: 0, Hex: 0x0, Operation:
2 | Decimal: 1, Hex: 0x1, Operation:
3 | Decimal: 0, Hex: 0x0, Operation:
4 | Decimal: 1, Hex: 0x1, Operation: +
5 | Decimal: 0, Hex: 0x0, Operation:
6 | Decimal: 1, Hex: 0x1, Operation:
7 | Decimal: 0, Hex: 0x0, Operation:
8 | Decimal: 2, Hex: 0x2, Operation:
9 | Decimal: 0, Hex: 0x0, Operation:
10 | Decimal: 1, Hex: 0x1, Operation:
11 | Decimal: 2, Hex: 0x2, Operation:
12 | Decimal: 1, Hex: 0x1, Operation: +
13 | Decimal: 2, Hex: 0x2, Operation:
14 | Decimal: 1, Hex: 0x1, Operation: +
15 | Decimal: 2, Hex: 0x2, Operation:
16 | Decimal: 3, Hex: 0x3, Operation: +
17 | Decimal: 4, Hex: 0x4, Operation: +
18 | Decimal: 5, Hex: 0x5, Operation:
19 | Decimal: 0, Hex: 0x0, Operation:
20 | Decimal: 2, Hex: 0x2, Operation:
21 | Decimal: 0, Hex: 0x0, Operation:
22 | Decimal: 1, Hex: 0x1, Operation: +
23 | Decimal: 2, Hex: 0x2, Operation: -
24 | Decimal: 0, Hex: 0x0, Operation:
25 | Decimal: 0, Hex: 0x0, Operation: +
26 | Decimal: 0, Hex: 0x0, Operation: +
27 | Decimal: 0, Hex: 0x0, Operation: +
28 | Decimal: 0, Hex: 0x0, Operation: +
29 | Decimal: 0, Hex: 0x0, Operation: +
30 | Decimal: 0, Hex: 0x0, Operation:
31 | Decimal: 0, Hex: 0x0, Operation: -
32 | Decimal: 0, Hex: 0x0, Operation: -
33 | Decimal: 0, Hex: 0x0, Operation: -
34 | Decimal: 0, Hex: 0x0, Operation: -
35 | Decimal: 0, Hex: 0x0, Operation: -
36 | Decimal: 0, Hex: 0x0, Operation:
37 | Decimal: 0, Hex: 0x0, Operation: *
38 | Decimal: 0, Hex: 0x0, Operation: *
39 | Decimal: 0, Hex: 0x0, Operation: *
40 | Decimal: 0, Hex: 0x0, Operation: *
41 | Decimal: 0, Hex: 0x0, Operation: *
42 | Decimal: 0, Hex: 0x0, Operation:
43 | Decimal: 0, Hex: 0x0, Operation: /
44 | Decimal: 0, Hex: 0x0, Operation: /
45 | Decimal: 0, Hex: 0x0, Operation: /
46 | Decimal: 0, Hex: 0x0, Operation: /
47 | Decimal: 0, Hex: 0x0, Operation: /
48 | Decimal: 0, Hex: 0x0, Operation:
49 | Decimal: 0, Hex: 0x0, Operation: %
50 | Decimal: 0, Hex: 0x0, Operation: %
51 | Decimal: 0, Hex: 0x0, Operation: %
52 | Decimal: 0, Hex: 0x0, Operation: %
53 | Decimal: 0, Hex: 0x0, Operation: %
54 | Decimal: 0, Hex: 0x0, Operation:
55 | Decimal: 0, Hex: 0x0, Operation: &
56 | Decimal: 0, Hex: 0x0, Operation: &
57 | Decimal: 0, Hex: 0x0, Operation: &
58 | Decimal: 0, Hex: 0x0, Operation: &
59 | Decimal: 0, Hex: 0x0, Operation: &
60 | Decimal: 0, Hex: 0x0, Operation:
61 | Decimal: 0, Hex: 0x0, Operation: |
62 | Decimal: 0, Hex: 0x0, Operation: |
63 | Decimal: 0, Hex: 0x0, Operation: |
64 | Decimal: 0, Hex: 0x0, Operation: |
65 | Decimal: 0, Hex: 0x0, Operation: |
66 | Decimal: 0, Hex: 0x0, Operation:
67 | Decimal: 0, Hex: 0x0, Operation: $
68 | Decimal: 0, Hex: 0x0, Operation: $
69 | Decimal: 0, Hex: 0x0, Operation: $
70 | Decimal: -1, Hex: 0xffffffffffffffff, Operation: $
71 | Decimal: 0, Hex: 0x0, Operation: $
72 | Decimal: 0, Hex: 0x0, Operation:
73 | Decimal: 0, Hex: 0x0, Operation: ^
74 | Decimal: 0, Hex: 0x0, Operation: ^
75 | Decimal: 0, Hex: 0x0, Operation: ^
76 | Decimal: 0, Hex: 0x0, Operation: ^
77 | Decimal: 0, Hex: 0x0, Operation: ^
78 | Decimal: 0, Hex: 0x0, Operation:
79 | Decimal: -1, Hex: 0xffffffffffffffff, Operation:
80 | Decimal: 0, Hex: 0x0, Operation:
81 | Decimal: 0, Hex: 0x0, Operation:
82 | Decimal: 0, Hex: 0x0, Operation:
83 | Decimal: 0, Hex: 0x0, Operation:
84 | Decimal: 0, Hex: 0x0, Operation:
85 | Decimal: 0, Hex: 0x0, Operation: <
86 | Decimal: 0, Hex: 0x0, Operation: <
87 | Decimal: 0, Hex: 0x0, Operation: <
88 | Decimal: 0, Hex: 0x0, Operation: <
89 | Decimal: 0, Hex: 0x0, Operation: <
90 | Decimal: 0, Hex: 0x0, Operation:
91 | Decimal: 0, Hex: 0x0, Operation: >
92 | Decimal: 0, Hex: 0x0, Operation: >
93 | Decimal: 0, Hex: 0x0, Operation: >
94 | Decimal: 0, Hex: 0x0, Operation: >
95 | Decimal: 0, Hex: 0x0, Operation: >
96 | Decimal: 0, Hex: 0x0, Operation:
97 | Decimal: 0, Hex: 0x0, Operation: :
98 | Decimal: 0, Hex: 0x0, Operation: :
99 | Decimal: 0, Hex: 0x0, Operation: :
100 | Decimal: 0, Hex: 0x0, Operation: :
101 | Decimal: 0, Hex: 0x0, Operation: :
102 | Decimal: 0, Hex: 0x0, Operation:
103 | Decimal: 0, Hex: 0x0, Operation: ;
104 | Decimal: 0, Hex: 0x0, Operation: ;
105 | Decimal: 0, Hex: 0x0, Operation: ;
106 | Decimal: 0, Hex: 0x0, Operation: ;
107 | Decimal: 0, Hex: 0x0, Operation: ;
108 | Decimal: 0, Hex: 0x0, Operation:
109 | Decimal: 0, Hex: 0x0, Operation:
110 | Decimal: 0, Hex: 0x0, Operation:
111 | Decimal: 0, Hex: 0x0, Operation:
112 | Decimal: 0, Hex: 0x0, Operation:
113 | Decimal: 0, Hex: 0x0, Operation:
114 | Decimal: 0, Hex: 0x0, Operation:
115 | Decimal: 0, Hex: 0x0, Operation:
116 | Decimal: 0, Hex: 0x0, Operation:
117 | Decimal: 0, Hex: 0x0, Operation:
118 | Decimal: 0, Hex: 0x0, Operation:
119 | Decimal: 0, Hex: 0x0, Operation:
120 | Decimal: 0, Hex: 0x0, Operation:
121 | Decimal: 0, Hex: 0x0, Operation: +
122 | Decimal: 0, Hex: 0x0, Operation: -
123 | Decimal: 0, Hex: 0x0, Operation: *
124 | Decimal: 0, Hex: 0x0, Operation: /
125 | Decimal: 0, Hex: 0x0, Operation: %
126 | Decimal: 0, Hex: 0x0, Operation: &
127 | Decimal: 0, Hex: 0x0, Operation: |
128 | Decimal: 0, Hex: 0x0, Operation: $
129 | Decimal: 0, Hex: 0x0, Operation: ^
130 | Decimal: -1, Hex: 0xffffffffffffffff, Operation:
131 | Decimal: -1, Hex: 0xffffffffffffffff, Operation: <
132 | Decimal: -1, Hex: 0xffffffffffffffff, Operation: >
133 | Decimal: -1, Hex: 0xffffffffffffffff, Operation: :
134 | Decimal: -1, Hex: 0xffffffffffffffff, Operation: ;
135 | Decimal: 1, Hex: 0x1, Operation:
136 | Decimal: 72057594037927936, Hex: 0x100000000000000, Operation:
137 | Decimal: 0, Hex: 0x0, Operation:
138 | Decimal: 0, Hex: 0x0, Operation:
139 | Decimal: 0, Hex: 0x0, Operation:
140 |
--------------------------------------------------------------------------------
/tests/input-formats.test:
--------------------------------------------------------------------------------
1 | 1
2 |
3 | 1+
4 |
5 | +1
6 |
7 | 1+1
8 |
9 | 1
10 | +1
11 | 1+
12 | 1
13 | 1+
14 | +1
15 | +1+
16 | 1+
17 | 1
18 |
19 | 1++1
20 | 1+-1
21 | 1++
22 | 1+-
23 |
24 | +
25 | ++
26 | +++
27 | ++++
28 | +++++
29 |
30 | -
31 | --
32 | ---
33 | ----
34 | -----
35 |
36 | *
37 | **
38 | ***
39 | ****
40 | *****
41 |
42 | /
43 | //
44 | ///
45 | ////
46 | /////
47 |
48 | %
49 | %%
50 | %%%
51 | %%%%
52 | %%%%%
53 |
54 | &
55 | &&
56 | &&&
57 | &&&&
58 | &&&&&
59 |
60 | |
61 | ||
62 | |||
63 | ||||
64 | |||||
65 |
66 | $
67 | $$
68 | $$$
69 | $$$$
70 | $$$$$
71 |
72 | ^
73 | ^^
74 | ^^^
75 | ^^^^
76 | ^^^^^
77 |
78 | ~
79 | ~~
80 | ~~~
81 | ~~~~
82 | ~~~~~
83 |
84 | <
85 | <<
86 | <<<
87 | <<<<
88 | <<<<<
89 |
90 | >
91 | >>
92 | >>>
93 | >>>>
94 | >>>>>
95 |
96 | :
97 | ::
98 | :::
99 | ::::
100 | :::::
101 |
102 | ;
103 | ;;
104 | ;;;
105 | ;;;;
106 | ;;;;;
107 |
108 | _
109 | __
110 | ___
111 | ____
112 | _____
113 |
114 | @
115 | @@
116 | @@@
117 | @@@@
118 | @@@@@
119 |
120 | +
121 | -
122 | *
123 | /
124 | %
125 | &
126 | |
127 | $
128 | ^
129 | ~
130 | <
131 | >
132 | :
133 | ;
134 | _
135 | @
136 |
137 | +.*/%&|$^~<>:;_@
138 |
139 | exit
140 |
--------------------------------------------------------------------------------
/tests/number-bases.correct:
--------------------------------------------------------------------------------
1 | Decimal: 0, Hex: 0x0, Operation:
2 | Decimal: 1, Hex: 0x1, Operation:
3 | Decimal: 1, Hex: 0x1, Operation:
4 | Decimal: 1, Hex: 0x1, Operation:
5 | Decimal: 1, Hex: 0x1, Operation:
6 | Decimal: 1, Hex: 0x1, Operation:
7 | Decimal: 12, Hex: 0xc, Operation:
8 | Decimal: 18, Hex: 0x12, Operation:
9 | Decimal: 18, Hex: 0x12, Operation:
10 | Decimal: 2, Hex: 0x2, Operation:
11 | Decimal: 2, Hex: 0x2, Operation:
12 |
--------------------------------------------------------------------------------
/tests/number-bases.test:
--------------------------------------------------------------------------------
1 | 1
2 | 0x1
3 | x1
4 | 0b1
5 | b1
6 | 12
7 | 0x12
8 | x12
9 | 0b10
10 | b10
11 | exit
12 |
--------------------------------------------------------------------------------
/tests/random.correct:
--------------------------------------------------------------------------------
1 | Decimal: 0, Hex: 0x0, Operation:
2 | Decimal: 2, Hex: 0x2, Operation:
3 | Decimal: 45, Hex: 0x2d, Operation:
4 | Decimal: 2, Hex: 0x2, Operation:
5 | Decimal: 2, Hex: 0x2, Operation:
6 | Decimal: 2, Hex: 0x2, Operation: -
7 | Decimal: 1, Hex: 0x1, Operation:
8 | Decimal: 3, Hex: 0x3, Operation:
9 | Decimal: -1, Hex: 0xffffffffffffffff, Operation:
10 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
11 | Decimal: -3, Hex: 0xfffffffffffffffd, Operation:
12 | Decimal: -1, Hex: 0xffffffffffffffff, Operation:
13 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
14 | Decimal: 4, Hex: 0x4, Operation:
15 | Decimal: 3, Hex: 0x3, Operation:
16 | Decimal: 7, Hex: 0x7, Operation:
17 | Decimal: 2, Hex: 0x2, Operation:
18 | Decimal: 4, Hex: 0x4, Operation:
19 | Decimal: 3, Hex: 0x3, Operation:
20 | Decimal: 0, Hex: 0x0, Operation:
21 | Decimal: 2, Hex: 0x2, Operation:
22 | Decimal: 1, Hex: 0x1, Operation:
23 | Decimal: 2, Hex: 0x2, Operation:
24 | Decimal: 1, Hex: 0x1, Operation:
25 | Decimal: 2, Hex: 0x2, Operation:
26 | Decimal: 2, Hex: 0x2, Operation:
27 | Decimal: 5, Hex: 0x5, Operation:
28 | Decimal: 5, Hex: 0x5, Operation:
29 | Decimal: 4, Hex: 0x4, Operation:
30 | Decimal: 5, Hex: 0x5, Operation:
31 | Decimal: 103495, Hex: 0x19447, Operation:
32 | Decimal: 0, Hex: 0x0, Operation:
33 | Decimal: 1345, Hex: 0x541, Operation:
34 | Decimal: 1468, Hex: 0x5bc, Operation: +
35 | Decimal: 1455, Hex: 0x5af, Operation:
36 | Decimal: 242, Hex: 0xf2, Operation:
37 | Decimal: 242, Hex: 0xf2, Operation:
38 | Decimal: 241, Hex: 0xf1, Operation:
39 | Decimal: 240, Hex: 0xf0, Operation:
40 | Decimal: 240, Hex: 0xf0, Operation:
41 | Decimal: 256, Hex: 0x100, Operation:
42 | Decimal: 128, Hex: 0x80, Operation:
43 | Decimal: 64, Hex: 0x40, Operation:
44 | Decimal: 32, Hex: 0x20, Operation:
45 | Decimal: 16, Hex: 0x10, Operation:
46 | Decimal: 256, Hex: 0x100, Operation:
47 | Decimal: 0, Hex: 0x0, Operation:
48 | Decimal: 256, Hex: 0x100, Operation:
49 | Decimal: 0, Hex: 0x0, Operation:
50 | Decimal: 256, Hex: 0x100, Operation:
51 | Decimal: 256, Hex: 0x100, Operation:
52 | Decimal: 256, Hex: 0x100, Operation:
53 | Decimal: 256, Hex: 0x100, Operation:
54 | Decimal: 256, Hex: 0x100, Operation:
55 | Decimal: 257, Hex: 0x101, Operation:
56 | Decimal: 256, Hex: 0x100, Operation:
57 | Decimal: 257, Hex: 0x101, Operation:
58 | Decimal: 258, Hex: 0x102, Operation:
59 | Decimal: 259, Hex: 0x103, Operation:
60 | Decimal: 258, Hex: 0x102, Operation:
61 | Decimal: 259, Hex: 0x103, Operation:
62 | Decimal: 258, Hex: 0x102, Operation:
63 | Decimal: 257, Hex: 0x101, Operation:
64 | Decimal: 513, Hex: 0x201, Operation:
65 | Decimal: 512, Hex: 0x200, Operation:
66 | Decimal: 256, Hex: 0x100, Operation:
67 | Decimal: 512, Hex: 0x200, Operation:
68 | Decimal: 1024, Hex: 0x400, Operation:
69 | Decimal: 2048, Hex: 0x800, Operation:
70 | Decimal: 2048, Hex: 0x800, Operation:
71 | Decimal: 4096, Hex: 0x1000, Operation:
72 | Decimal: 4096, Hex: 0x1000, Operation:
73 | Decimal: 16384, Hex: 0x4000, Operation:
74 | Decimal: 262144, Hex: 0x40000, Operation:
75 | Decimal: 8388608, Hex: 0x800000, Operation:
76 | Decimal: 16777216, Hex: 0x1000000, Operation:
77 | Decimal: 33554432, Hex: 0x2000000, Operation:
78 | Decimal: 67108864, Hex: 0x4000000, Operation:
79 | Decimal: 134217728, Hex: 0x8000000, Operation:
80 | Decimal: 268435456, Hex: 0x10000000, Operation:
81 | Decimal: 536870912, Hex: 0x20000000, Operation:
82 | Decimal: 1073741824, Hex: 0x40000000, Operation:
83 | Decimal: 2147483648, Hex: 0x80000000, Operation:
84 | Decimal: 4294967296, Hex: 0x100000000, Operation:
85 | Decimal: 8589934592, Hex: 0x200000000, Operation:
86 | Decimal: 17179869184, Hex: 0x400000000, Operation:
87 | Decimal: 34359738368, Hex: 0x800000000, Operation:
88 | Decimal: 68719476736, Hex: 0x1000000000, Operation:
89 | Decimal: 137438953472, Hex: 0x2000000000, Operation:
90 | Decimal: 549755813888, Hex: 0x8000000000, Operation:
91 | Decimal: 2199023255552, Hex: 0x20000000000, Operation:
92 | Decimal: 4398046511104, Hex: 0x40000000000, Operation:
93 | Decimal: 8796093022208, Hex: 0x80000000000, Operation:
94 | Decimal: 17592186044416, Hex: 0x100000000000, Operation:
95 | Decimal: 35184372088832, Hex: 0x200000000000, Operation:
96 | Decimal: 140737488355328, Hex: 0x800000000000, Operation:
97 | Decimal: 1125899906842624, Hex: 0x4000000000000, Operation:
98 | Decimal: 36028797018963968, Hex: 0x80000000000000, Operation:
99 | Decimal: 1152921504606846976, Hex: 0x1000000000000000, Operation:
100 | Decimal: 4611686018427387904, Hex: 0x4000000000000000, Operation:
101 | Decimal: 1, Hex: 0x1, Operation:
102 | Decimal: -9223372036854775808, Hex: 0x8000000000000000, Operation:
103 | Decimal: 1, Hex: 0x1, Operation:
104 | Decimal: 2, Hex: 0x2, Operation:
105 | Decimal: -9223372036854775808, Hex: 0x8000000000000000, Operation:
106 | Decimal: 4, Hex: 0x4, Operation:
107 | Decimal: -9223372036854775808, Hex: 0x8000000000000000, Operation:
108 | Decimal: -9223372036854775808, Hex: 0x8000000000000000, Operation:
109 | Decimal: -9223372036854775808, Hex: 0x8000000000000000, Operation:
110 | Decimal: 9223372036854775807, Hex: 0x7fffffffffffffff, Operation:
111 | Decimal: -9223372036854775807, Hex: 0x8000000000000001, Operation:
112 | Decimal: 9223372036854775807, Hex: 0x7fffffffffffffff, Operation:
113 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
114 | Decimal: 1, Hex: 0x1, Operation:
115 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
116 | Decimal: 2, Hex: 0x2, Operation:
117 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
118 | Decimal: 1, Hex: 0x1, Operation:
119 | Decimal: -1, Hex: 0xffffffffffffffff, Operation:
120 | Decimal: 1, Hex: 0x1, Operation:
121 | Decimal: 256, Hex: 0x100, Operation:
122 | Decimal: -257, Hex: 0xfffffffffffffeff, Operation:
123 | Decimal: 256, Hex: 0x100, Operation:
124 | Decimal: -256, Hex: 0xffffffffffffff00, Operation:
125 | Decimal: 256, Hex: 0x100, Operation:
126 | Decimal: 1, Hex: 0x1, Operation:
127 | Decimal: 1, Hex: 0x1, Operation: $
128 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
129 | Decimal: 0, Hex: 0x0, Operation:
130 | Decimal: 1, Hex: 0x1, Operation:
131 | Decimal: 0, Hex: 0x0, Operation:
132 | Decimal: 1, Hex: 0x1, Operation:
133 | Decimal: 3, Hex: 0x3, Operation:
134 | Decimal: 1, Hex: 0x1, Operation:
135 | Decimal: 2, Hex: 0x2, Operation:
136 | Decimal: 0, Hex: 0x0, Operation:
137 | Decimal: 2, Hex: 0x2, Operation:
138 | Decimal: 2, Hex: 0x2, Operation:
139 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
140 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
141 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
142 | Decimal: 0, Hex: 0x0, Operation:
143 | Decimal: 1, Hex: 0x1, Operation: -
144 | Decimal: 1, Hex: 0x1, Operation:
145 | Decimal: 2, Hex: 0x2, Operation:
146 | Decimal: 0, Hex: 0x0, Operation:
147 | Decimal: 1, Hex: 0x1, Operation:
148 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
149 | Decimal: -1, Hex: 0xffffffffffffffff, Operation:
150 | Decimal: 0, Hex: 0x0, Operation:
151 | Decimal: 0, Hex: 0x0, Operation:
152 | Decimal: 1, Hex: 0x1, Operation:
153 | Decimal: 1, Hex: 0x1, Operation:
154 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
155 | Decimal: 0, Hex: 0x0, Operation:
156 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
157 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
158 | Decimal: 0, Hex: 0x0, Operation:
159 | Decimal: 4, Hex: 0x4, Operation:
160 | Decimal: 0, Hex: 0x0, Operation:
161 | Decimal: 2, Hex: 0x2, Operation:
162 | Decimal: 6, Hex: 0x6, Operation:
163 | Decimal: 24, Hex: 0x18, Operation:
164 | Decimal: 12, Hex: 0xc, Operation:
165 | Decimal: 6, Hex: 0x6, Operation:
166 | Decimal: 3, Hex: 0x3, Operation:
167 | Decimal: 12, Hex: 0xc, Operation:
168 | Decimal: 96, Hex: 0x60, Operation:
169 | Decimal: 1536, Hex: 0x600, Operation:
170 | Decimal: 49152, Hex: 0xc000, Operation:
171 | Decimal: 4, Hex: 0x4, Operation:
172 | Decimal: 8, Hex: 0x8, Operation:
173 | Decimal: -8, Hex: 0xfffffffffffffff8, Operation:
174 | Decimal: 7, Hex: 0x7, Operation:
175 | Decimal: 7, Hex: 0x7, Operation:
176 | Decimal: 7, Hex: 0x7, Operation:
177 | Decimal: 1, Hex: 0x1, Operation:
178 | Decimal: -2, Hex: 0xfffffffffffffffe, Operation:
179 | Decimal: 2, Hex: 0x2, Operation:
180 | Decimal: 2, Hex: 0x2, Operation: +
181 | Decimal: 2, Hex: 0x2, Operation: +
182 |
--------------------------------------------------------------------------------
/tests/random.test:
--------------------------------------------------------------------------------
1 | 1+1
2 | 1+2+3+4+5+6+7+8+9
3 | 1+++++++++1
4 | 1----------------1
5 | 1+1-
6 | 1
7 | 1--2
8 | 1+-2
9 | -1
10 | -1
11 | (-1)
12 | (-1+-1)
13 | (1+1)*2
14 | 1+1*2
15 | 1+2*3
16 | 1<1
17 | 1<1+1
18 | (1<1)+1
19 | 1<1&1
20 | 1<(1&1)
21 | 1/1
22 | 2/1
23 | 2/2
24 | 3/2*2
25 | 3%2*2
26 | 5&5
27 | 5|5
28 | 5 & 4
29 | 4 | 5
30 | 5 | 103495
31 | &0
32 | |1345
33 | +123+
34 | -13
35 | /6
36 | %256
37 | +-1
38 | -1
39 | +&1
40 | +16
41 | >1
42 | >1
43 | >1
44 | >1
45 | 256
46 | <63
47 | 256
48 | >63
49 | 256
50 | >0
51 | <0
52 | <<<<0
53 | >>>>>>>0
54 | ++++++++++1
55 | ------------1
56 | -(-1)
57 | --1
58 | ---1
59 | ----1
60 | -----1
61 | ------1
62 | -1
63 | +256
64 | -1
65 | ;1
66 | :1
67 | :1
68 | :1
69 | :::::::1
70 | :1
71 | :::::1
72 | :2
73 | :4
74 | :5
75 | :1
76 | :1
77 | :1
78 | :1
79 | :1
80 | :1
81 | :1
82 | :1
83 | :1
84 | :1
85 | :1
86 | :1
87 | :1
88 | :1
89 | :2
90 | :2
91 | :1
92 | :1
93 | :1
94 | :1
95 | :2
96 | :3
97 | :5
98 | :5
99 | :2
100 | :2
101 | ;1
102 | :1
103 | :1
104 | ;2
105 | :3
106 | ;3
107 | _
108 | _
109 | -1
110 | _
111 | _
112 | :1
113 | ~
114 | ~
115 | _
116 | _
117 | ~
118 | _
119 | _
120 | +255
121 | ~
122 | ~
123 | _
124 | _
125 | 1
126 | $
127 | 1
128 | $1
129 | 1
130 | ^1
131 | ^1
132 | |3
133 | ^2
134 | ^3
135 | ^2
136 | 3^2^3
137 | ^0
138 | 1+1-2*-1/1%2&1|1$1^1
139 | 2*-1
140 | /1
141 | %2
142 | 1-
143 | 0
144 | +1
145 | &1
146 | ^1
147 | $1
148 | |1
149 |
150 |
151 | 1
152 | |1
153 | $1
154 | ^-2
155 | ^-2
156 | 3^-2^3
157 | 5^1^4
158 | 5^1
159 | ^4
160 | 4^2^4
161 | ^4
162 | <2
163 | >1
164 | ;1
165 | ;1
166 | :2
167 | :3
168 | :4
169 | :5
170 | (1+1)*2
171 | *2
172 | _
173 | ~
174 | %8
175 | %9
176 | %6
177 | $1
178 | _
179 | ++
180 | +++
181 | exit
182 |
--------------------------------------------------------------------------------