-
Notifications
You must be signed in to change notification settings - Fork 1
constants and functions in Python stdlib #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Will review after next Monday when my exams end. |
Good luck! |
/* | ||
Create a map to hold built-in functions. | ||
The keys are strings (function names) and the values are functions that can take any arguments. | ||
*/ | ||
export const builtIns = new Map<string, (...args: any[]) => any>(); | ||
builtIns.set('_int', _int); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please put this into a JSON file? Called py_s1_constants.json
or something like that?
That will make switching between Python S1 and Python s2 easier and also separate the code out clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion! I’ll make the change to use a JSON file as you proposed.
expectedType = 'string'; | ||
break; | ||
case 'bool': | ||
expectedType = 'bool'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isinstance(True, int)
is true in Python (bool is a subclass of int in CPython). Do we want to allow that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No... we follow Source Chapter 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, our current definition of "sublanguage" is like this:
A program that runs successfully using Python §1 in Source Academy should run successfully in the official Python implementation and produce the same result. (assuming that the correct sicp library is loaded that provides things like Math_sin)
By that reasoning, if we include isinstance in Python §1, any call of isinstance that succeeds in Python §1 should give the same result.
So currently, does isinstance(True, int) give an error, or does it give False in Python §1? An error would be consistent with our sublanguage notion, but False would not be.
export function math_acosh(args: Value[], source: string, command: ControlItem, context: Context): Value { | ||
if (args.length < 1) { | ||
handleRuntimeError(context, new MissingRequiredPositionalError(source, (command as es.Node), 'math_acosh', Number(1), args, false)); | ||
} else if (args.length > 1) { | ||
handleRuntimeError(context, new TooManyPositionalArgumentsError(source, (command as es.Node), 'math_acosh', Number(1), args, false)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that for each of this, the argument parsing is mostly the same.
If so, can we add a decorator to do the argument parsing? Basically take a higher order function https://www.typescriptlang.org/docs/handbook/decorators.html.
Something like
@validate(1, "math_acosh") // 1 is the number of expected arguments.
export function math_acosh(...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great suggestion — I’ll implement a @Validate decorator to handle the argument checking. Thanks for pointing it out!
The detailed documentation for Python Chapter 1 stdlib is available at:
https://wangyuyang1013.github.io/py-slang-bak-2/docs/python/python_1/global.html
For the convenience of steps demonstration, I have temporarily pushed it to my personal GitHub repository.
Python Chapter 1 implements most of the Python
math
library and some other built-in functions. Some functions were not included because their return types are not supported in Python Chapter 1 (e.g.,tuple
), or because certain parameters only support keyword arguments (also not supported in Python Chapter 1).Most of the planned functions have been implemented. Only a few, such as
nextafter
andulp
, are still pending due to the complexity of implementing them without precision loss, andinput
, as the conductor developer has not yet provided a way to read user input from the REPL.