ʰ
Size: a a a
ʰ
ʰ
𝘎(
ʰ
ʰ
PP
MT
ʰ
(import 'lib/println.rf' 'lib/method.rf')
(def rectangle x y
(ret
x &x
y &y
perimeter (lambda
(* (+
(struct x &args)
(struct y &args)
) 2)
)
area (lambda
(* (struct x &args) (struct y &args))
)
)
)
(def square a
(rectangle &a &a &args)
)
(def describe
(set x (struct x &args))
(set y (struct y &args))
(println
(if (== &x &y)
(ret 'Квадрат со стороной' &x)
else
(ret 'Прямоугольник' &x на &y)
)
(str '(Площадь: ' (method area &args) ')')
(str '(Периметр: ' (method perimeter &args) ')')
)
)
(set p1 (rectangle 5 4))
(set p2 (square 8))
(describe &p1)
(describe &p2)
(import 'lib/println.rf' 'lib/method.rf')
(def rectangle x y
(ret
x &x
y &y
perimeter (lambda self
(* (+
(struct x &self)
(struct y &self)
) 2)
)
area (lambda self
(* (struct x &self) (struct y &self))
)
scale (lambda self s
(rectangle
(* (struct x &self) &s)
(* (struct y &self) &s)
)
)
)
)
(def square a
(rectangle &a &a)
)
(def describe rect
(set x (struct x &rect))
(set y (struct y &rect))
(println
(if (== &x &y)
(ret 'Квадрат со стороной' &x)
else
(ret 'Прямоугольник' &x на &y)
)
(str '(Площадь: ' (method area &rect) ')')
(str '(Периметр: ' (method perimeter &rect) ')')
)
)
(set p1 (rectangle 5 4))
(set p2 (square 8))
(describe &p1)
(describe &p2)
(set p3 (method scale &p1 5))
(describe &p3)
ʰ
(def method f o
(run (struct &f &o) (get o) &args)
)
ʰ
R
ʰ
Прямоугольник 5 на 4 (Площадь: 20) (Периметр: 18)
Квадрат со стороной 8 (Площадь: 64) (Периметр: 32)
Прямоугольник 25 на 20 (Площадь: 500) (Периметр: 90)
ʰ
AD
ʰ
char *concat_with_standard_lib_directory(const char *relative_path) {
#ifndef _WIN32
#define LIB_FOLDER "/.local/lib/rafael/"
char *basedir = getenv("HOME");
if (basedir == NULL)
return NULL;
#else
#define LIB_FOLDER "/rafael/lib/"
char *basedir = getenv("localappdata");
if (basedir == NULL)
return NULL;
#endif
char *path = malloc((strlen(basedir) + strlen(relative_path)) * sizeof(char) + sizeof(LIB_FOLDER));
*path = '\0';
return strcat(strcat(strcat(path, basedir), LIB_FOLDER), relative_path);
#undef LIB_FOLDER
}
ʰ
if (access(path, F_OK) != 0) {
free(path);
path = concat_with_standard_lib_directory(relative_path);
if (path == NULL) {
EXCEPTION(ret, "File %s doesn't seem to exist (cannot obtain standard library)", path);
} if (access(path, F_OK) != 0) {
EXCEPTION(ret, "File %s doesn't seem to exist", path);
}
}
ʰ