Hi Programming Language: starting to design

We are starting to design the Hi programming language . Here is an introduction and some answers to questions about why you need it. However, and for no good reason, language design is an unusually interesting task if you take it seriously.



Following our thesis of maximum accessibility for learning, we will try to logically integrate all the basic elements of the language from the widely used elements of other algorithmic languages. Here we follow the internal logic given in the first article and the assumption that the composition of the entities of several popular languages ​​will make it easier to grasp new content with less effort, since people have a built-in integrating machine based on neural networks that automatically introduces semantics parte violations. soobscheni.



The idea of ​​constructing new languages ​​from elements already known has a long history. More than 140 years ago, Dr. Zamenhof created the artificial language of Esperanto from the constituent parts of other natural languages. The language turned out to be so logical and easy to learn that it turned out to be viable and survived to this day. For example, it is available in Google translator. Saluton Habr! Bonvenon sub la kat!



Let's try to design a language that is convenient for quick programming of intellectual games and puzzles, and the use of which will please with its logic and thoughtfulness.



Comments



For a one-line comment, use the # symbol (Python, R, Ruby). The combination // (C and many other languages) can be used as an integer division operator to get the result of an operation as a real number, so as not to use in this case a slightly cumbersome conversion expression like CGFloat (int), as in Swift.



PRINT “Hi world\n”
#  Hi world     


To highlight multi-line comments, use the ## combination .

##   .
,      Python ##


The choice of the # symbol is also convenient because in the future it will be used by the Hi language interpreter to process meta tags in the program text, which can be synthesized from the comment text. This is a bit like the preprocessor in C / C ++, which, by the way, also uses the # symbol.



Elementary data types



Hi language in basic edition uses three elementary data types:



Binary: BOOL

Possible operations: & (and), | (or), ~ (not)

LET true = TRUE 
LET false = FALSE
PRINT true & true   #  TRUE
PRINT true | false   #  TRUE
PRINT ~true   #  FALSE


Integers: INT

Possible operations: + , - , * , / (integer division), % (remainder), // (division with automatic conversion of the result to the REAL type)

VAR i = 0
PRINT (i + 6 – 2) * 3 / 5   #  2


Real numbers: REAL

Possible operations: + , - , * , /

VAR pi = 3.1415926535


Strings



Strings are a composite type because they are treated as a string of Unicode characters

LET the_force = “flow through you”


We do not use single quotes for string literals, since characters, especially those located on the standard keyboard, are a valuable resource for the future.

Standard string operations:

# 
VAR language = “C”
LET statement  = “ – the best language ever”
LET proposition = language + statement
language = “Python”


We don't need to introduce a data type of the Character type yet; to solve practical problems, you can use ...

#    
LET secondSym = language[2]


The result of the assignment in the case when the reference by the index is outside the string is “” and does not lead to a run-time error.



Constants and Variables



As we saw in the examples above, Hi is a statically typed language to reduce the likelihood of errors in the use of identifiers and a better understanding of their purpose. Constants ( LET ) and variables ( VAR ) must be declared before the first use of the object. It is more ergonomic to declare types without explicitly specifying a specific type by assigning initial values; in this case, the type inference becomes obvious.

##  
LET one: INT = 1  : ##
LET one = 1
VAR boolean = TRUE


The concept of declaring constants and variables let and var is borrowed from Swift, but in Hi it is possible to make multiple declarations with the assignment of one value:

VAR i, j, k = 0


Standard input and output



Data input from the keyboard is carried out by the INPUT command :

VAR startFrom: INT
PRINT "Start from: "; INPUT startFrom


You do not need to type true or false to enter a BOOL value. The mobile application for programming, which we are preparing for release in the AppStore, is smart enough to display only two buttons with a binary choice in this case. Likewise, a numeric keypad without a period for entering an integer and a regular keyboard for entering a text value will be displayed.



The PRINT command prints lines to a console display type screen. The default is 20 lines of 40 characters. You can select the mode with large characters 10 X 20 or small: 40 X 80.

PRINT "     Hi language"


Hi Calculator Language



Now we have everything to form a grammar of a calculator with interactive input-output in the form of EBNF in terms of the Hi language.

1. The Syntax of HI Programming Language

digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9".
capital = "A" | ... | "Z".
small = "a" | ... | "z".
character = [Unicode_character].
addOperator = "+" | "-" | "|".
mulOperator = "*" | "/" | "//" | "%" | "&".
relation = "==" | "~=" | "<" | "<=" | ">" | ">=".

integer = digit {digit}.
real = digit {digit} "." {digit}.
string = """ {character} """.

number = integer | real.
letter = capital | small.

keyword = capital capital {capital}.
ident = letter {letter | digit}.
type = keyword.

factor = number | string | TRUE | FALSE | ident | "(" expression ")" | "~" factor.
term = factor {mulOperator factor}.
simpleExpression = ["-"] term {addOperator term}.
expression = simpleExpression [relation simpleExpression].
assignment = ident "=" expression.

identList = ident {"," ident}.
expList = expression {"," expression}.
constDeclaration = LET identList [":" type] "=" expression.
varDeclaration = VAR identList [":" type] ["=" expression].
funcCall = keyword [expList ].

statement = assignment | funcCall | constDeclaration | varDeclaration 
statementSequence = statement {("\n" | ";") statement}.

2. Standard Data Types

BOOL	INT	REAL	STRING

3. Standard Procedures and Functions

INPUT	PRINT

4. Other Keywords

LET	VAR


Unlike the grammar of many languages, where keywords are included in the list of exceptions from a more general class of identifiers, we introduce a special kind of token - keyword, which is recognized already at the level of the Hi language interpreter scanner.



In the next article, we will introduce composite data types in Hi in addition to STRING. For our simple language, they are powerful enough to easily express ideas of manipulating object sets, arrays and dictionaries.



All Articles