-
Notifications
You must be signed in to change notification settings - Fork 0
Language namespaces
Derek Snider edited this page Jun 2, 2026
·
1 revision
Language namespaces are madc's signature feature. Use the best functions from PHP, Perl, Python, Ruby, JavaScript, and Rust — all in the same program.
Every namespaced function is called with the namespace::function() syntax:
php::trim(s);
perl::chomp(s);
python::title(s);
ruby::squeeze(s);
js::btoa(encoded, s);
rust::trim(s);These are built into madc. No imports, no packages, no installation required.
| Namespace | Functions | Focus | Reference |
|---|---|---|---|
| php:: | 36 | String manipulation, array operations (explode, implode, sort) | Full list → |
| perl:: | 21 | chop/chomp, regex grep, glob, split/join | Full list → |
| python:: | 16 | Title case, alignment (center/ljust/rjust/zfill), format | Full list → |
| ruby:: | 12 | squeeze, tr (transliterate), chars, rotate, compact | Full list → |
| js:: | 6 | Base64 (btoa/atob), URL encoding, parseInt, JSON stringify | Full list → |
| rust:: | 18 | trim/contains/replace, split/join, first/last/get, push/pop | Full list → |
Plus two built-in namespaces:
| Namespace | Functions | Focus |
|---|---|---|
std:: |
5 | cout, cin, cerr, endl, for_each |
madc:: |
4 | array, regex_match, regex_search, regex_replace |
By default, unqualified function names resolve through the normal C/madc lookup. The prefer directive changes lookup order so namespace functions can be called without a prefix:
int64_t len(string s) {
return 99;
}
int main() {
string s = "hello";
cout << len(s) << endl; // 99 (local function)
prefer rust, c;
cout << len(s) << endl; // 5 (rust::len wins)
}Two equivalent forms:
prefer rust, php, c; // statement form
#pragma prefer rust, php, c // preprocessor form-
cin the list means the normal madc/C lexical and global lookup path - Explicit
namespace::function()calls always resolve explicitly, regardless ofpreferorder -
prefertakes effect from that point forward in the file
Import namespace members into the current scope:
using namespace std; // import everything from std::
using std::cout; // import just coutLoad any shared library as a namespace:
#load "libc.so.6" as libc;
int main() {
int result;
result = libc::abs(-42);
cout << result << endl; // 42
return 0;
}Functions are resolved lazily via dlsym on first use. See Preprocessor for more on #load.
#include <iostream>
int main() {
string data = " Alice, Bob, Charlie ";
// PHP: trim whitespace, split on comma
php::trim(data);
array names;
string delim = ", ";
php::explode(names, delim, data);
// Perl: grep for names starting with 'B'
array b_names;
string pat = "^B";
perl::grep(b_names, pat, names);
// Python: title-case a greeting
string greeting = "hello from madc";
python::title(greeting);
cout << greeting << endl;
// Ruby: squeeze repeated chars
string s = "aabbccdd";
ruby::squeeze(s);
cout << s << endl;
// JS: base64 encode
string encoded;
js::btoa(encoded, s);
cout << encoded << endl;
// Rust: check contents
int has_b;
string needle = "bc";
has_b = rust::contains(s, needle);
cout << "contains 'bc': " << has_b << endl;
return 0;
}Explore each namespace in detail:
- php:: Namespace — 36 string and array functions
- perl:: Namespace — 21 text processing functions
- python:: Namespace — 16 string formatting functions
- ruby:: Namespace — 12 string transform functions
- js:: Namespace — 6 encoding and utility functions
- rust:: Namespace — 18 string and collection functions
- Regex — madc::regex_match, regex_search, regex_replace