Lesson 25 of 25 · Larger programs

The English and Persian glossary

Every Salam program can be written in English or in Persian. The two spellings go through the same compiler and follow the same rules, so this glossary is all you need to read a program in the other language or move one across.

One program, two languages

These two programs are the same, and produce the same output:

functions.salam
func fibonacci(n: int): int:
    if n < 2:
        ret n
    end
    ret fibonacci(n - 1) + fibonacci(n - 2)
end

func main:
    repeat 10 in i:
        print fibonacci(i), ""
    end
    println ""
end
Output0 1 1 2 3 5 8 13 21 34
functions_fa.salam
تابع فیبوناچی(ن: صحیح): صحیح:
    اگر ن < ۲:
        برگشت ن
    پایان
    برگشت فیبوناچی(ن - ۱) + فیبوناچی(ن - ۲)
پایان

تابع ریشه:
    تکرار ۱۰ در ک:
        چاپ فیبوناچی(ک), ""
    پایان
    سرچاپ ""
پایان
Output0 1 1 2 3 5 8 13 21 34

salam translate fa program.salam turns English code into Persian, and salam translate en goes the other way. It rewrites keywords, types, the entry function and the built-in methods. The compiler decides which language a file is in from its keywords, and a file uses one language throughout: in a Persian file the English spelling of a built-in name is an error, and the message names the Persian one.

Keywords

EnglishPersianEnglishPersian
funcتابعretبرگشت
mainریشهendپایان
ifاگرelseوگرنه
repeatتکرارuntil / toتا
each / byهرinدر
breakبشکنcontinueگذر
matchهمخوانswitchترابرد
mutناپایاconstپایا
structساختارenumجداشمار
typeگونهinterfaceمیانجی
implکاربستonبر
pubهمگانیthisاین
asبرگردانdeferدیرکن
trueدرستfalseنادرست
nullپوچinputورودی
printچاپprintlnسرچاپ
printerrنادرست‌چاپprinterrlnنادرست‌سرچاپ
andوorیا
notوارونهeq / neqبرابر / نابرابر
lt / gtکوچکتر / بزرگترlte / gteکوچکتربرابر / بزرگتربرابر
packageبستهimportواردسازی
includeفراخوانیexternدرون‌داد
exportبرون‌دادlayoutچیدمان
componentبخشpureناب
inlineدرخطnoinlineنادرخط
noretنابرگشتdeprecatedبی‌کاره

The comparison words eq, neq, lt, gt, lte and gte stand for ==, !=, <, >, <= and >=, and both forms work in both languages. In Persian, تا is until at the start of a statement and to inside a repeat header, and هر is each at the start of a statement and the step by inside a repeat header. main is not a keyword but the name of the entry function, which a Persian program calls ریشه.

Context words

These words are keywords only in one position, so they can still be used as names elsewhere:

EnglishPersianEnglishPersian
staticایستاlinkپیوند
dynamicپویاframeworkچارچوب
useشاملdynپویا
mut funcناپایا تابعdefinedتعریف‌شده

static appears in static func and in link static. Persian پویا has two jobs: after پیوند it means dynamic, and in front of an interface type it means dyn. defined(NAME) belongs to conditional compilation.

Types

EnglishPersianEnglishPersian
voidتهیboolمنطقی
charنویسهucharیونیکد
strرشتهint / i32صحیح / صحیح۳۲
i8 / i16صحیح۸ / صحیح۱۶i64صحیح۶۴
uint / u32طبیعی / طبیعی۳۲u8 / u16طبیعی۸ / طبیعی۱۶
u64طبیعی۶۴usizeاندازه مثبت
sizeاندازهfloat / f32اعشار / اعشار۳۲
f64اعشار۶۴Vectorوکتور
HashMapنگاشتMapIterپیمایشگرنگاشت
FileپروندهVariantگوناگون
func (as a type)تابعdynپویا

Watch the floating-point types in Persian: اعشار on its own is the 32-bit float, and a literal such as ۲.۵ is an اعشار۶۴. The digits in a type name can be Persian or ASCII, so صحیح۶۴ and صحیح64 are the same type.

Built-in functions

A handful of functions need no import. A Persian program must use their Persian names:

EnglishPersianEnglishPersian
lenطولsizeofاندازه‌گونه
hashدرهمopenبازکردن
argsآرگومان‌هاlangزبان
listdirفهرست‌پوشهchar_codeکدنویسه
char_from_codeنویسه‌ازکدstrcmpمقایسه‌رشته
spawnنخ‌سازjoinنخ‌پیوند
callhandlerفراخوان‌دستگیرهatomic_loadاتمی‌بخوان
atomic_storeاتمی‌بنویسatomic_addاتمی‌بیفزا
atomic_swapاتمی‌جابجاatomic_casاتمی‌مقایسه

Most of them appear in earlier lessons: len on arrays and strings, spawn and join in Concurrency. sizeof takes a type, char_code gives the code of the first letter of a string, and lang() tells a program which language its own source is written in. This example uses several of them, with a few built-in methods:

kw-builtins.salam
func main:
    word := "salam"
    println lang(), len(word), sizeof(i64)
    println char_code("A"), char_from_code(66), strcmp("ab", "ac") < 0

    mut names := Vector {} as Vector<str>
    defer names.free()
    names.push("sara")
    names.push("reza")
    println names.len(), names.get(1), names.pop()

    mut ages := HashMap {} as HashMap<str, int>
    defer ages.free()
    ages.put("sara", 31)
    println ages.has("sara"), ages.get("sara"), ages.size()

    text := "  hello world  ".trim()
    println text.starts_with("hello"), text.find("world"), text.substr(0, 5)
    println "سلام".len(), "سلام".char_count()
end
Outputen 5 8 65 B true 2 reza reza true 31 1 true 6 hello 8 4

Built-in methods

Strings, vectors, maps, files and map iterators have methods built into the language, and they have a Persian spelling as well:

EnglishPersianEnglishPersian
pushبیفزاpopدربیاور
getبگیرrefارجاع
setبنشانlenطول
capظرفیتfreeآزادکن
putدرجhasدارد
removeحذفsizeاندازه
iterپیمایشhas_nextداردبعدی
keyکلیدvalueمقدار
nextبعدیreadخواندن
readlineخواندن خطwriteنوشتن
seekجابجاییcloseببند
concatپیوستsubstrزیررشته
findبیابsplitبشکاف
trimپیراستto_intبه صحیح
to_floatبه اعشارchar_countشمارنویسه
char_atنویسه شمارهchar_substrزیررشته نویسه
char_findبیاب نویسهstarts_withشروع با
ends_withختم باincludesدربردارد
copyرونوشتdeep_copyرونوشت عمیق

Two of the names count bytes, not letters: len and find work on the bytes of a string, so "سلام".len() is 8. Use char_count, char_at and char_find to work letter by letter, as the last line of the example above shows.

Compile-time constants

The constants of conditional compilation have Persian names too, and a Persian program must use them:

EnglishPersianEnglishPersian
SALAM_OS_WINDOWSسلام سیستم ویندوزSALAM_OS_WIN64سلام سیستم ویندوز۶۴
SALAM_OS_WIN32سلام سیستم ویندوز۳۲SALAM_OS_WASMسلام سیستم وب‌اسمبلی
SALAM_OS_ANDROIDسلام سیستم اندرویدSALAM_OS_LINUXسلام سیستم لینوکس
SALAM_OS_MACسلام سیستم مکSALAM_OS_BSDسلام سیستم بی‌اس‌دی
SALAM_OS_FREEBSDسلام سیستم فری‌بی‌اس‌دیSALAM_OS_OPENBSDسلام سیستم اوپن‌بی‌اس‌دی
SALAM_OS_NETBSDسلام سیستم نت‌بی‌اس‌دیSALAM_OS_UNIXسلام سیستم یونیکس
SALAM_OS_UEFIسلام سیستم یوئی‌اف‌آیSALAM_OS_SALAMOSسلام سیستم سلام
SALAM_ARCH_X64سلام معماری ایکس۶۴SALAM_ARCH_X86سلام معماری ایکس۸۶
SALAM_ARCH_ARM64سلام معماری آرم۶۴SALAM_ARCH_ARMسلام معماری آرم
SALAM_ARCH_WASMسلام معماری وب‌اسمبلیSALAM_MEM_DEBUGسلام اشکال‌زدایی حافظه
SALAM_ASANسلام پاک‌ساز نشانیSALAM_SAFEسلام ایمن
SALAM_FREESTANDINGسلام خودایستاSALAM_KERNELسلام هسته
SALAM_JSسلام جاوااسکریپتSALAM_OSسلام سیستم
SALAM_ARCHسلام معماریSALAM_VERSIONسلام نگارش
SALAM_VERSION_CODEسلام کد نگارشSALAM_GIT_COMMITسلام کامیت گیت
SALAM_GIT_DATEسلام تاریخ گیتSALAM_GIT_DIRTYسلام گیت ناپاک
SALAM_BUILD_DATEسلام تاریخ ساخت

SALAM_JS exists only when the program is compiled to JavaScript, so ask defined(SALAM_JS) rather than testing it directly. SALAM_GIT_DIRTY is text, "-dirty" for a compiler built from uncommitted sources.

Standard library packages and their functions have Persian names as well, such as رشته for str and ریاضی.جذر for math.Sqrt. Each package page in the library reference lists both names.