Data::SExpression

I was writing a compiler/parser/interpreter with perl but then I realized the way I'm doing it is almost completely wrong and bad (not tokenizing properly!)

Then someone told me that I should be using Data::SExpression because the language syntax is almost exact to LISP's (shown in the readmore. :) ) With the exception of comments (signified by the semicolon: ;)

My question is, how do I utilize SExpression for what I need to do?

I'm doing some tests right now with it but I'm still fairly confused, and would really appreciate a tutorial on it... My current code:

use warnings; my %hash_glob; my %hash_scrip; my @value_types = qw(boolean real short long string trigger_volume cut +scene_flag cutscene_camera_point cutscene_title cutscene_recording de +vice_group ai ai_command_list starting_profile conversation hud_message object_list sound effect damage looping_soun +d animation_graph actor_variant damage_effect object_definition game_difficulty team ai_default_state actor_type hud +_corner object unit vehicle weapon device scenery object_name unit_name vehicle_name weapon_name d +evice_name scenery_name); my @script_types = qw(startup dormant continuous static stub); my $glob_count = 0; my $pre_decl = " "; sub empty { my $FILTER = 'a-zA-Z0-9'; my $string = shift (@_); my $test = $string; $test =~ s/[^$FILTER]//go; if($test){ print "\n"; return 0; } else { print "\nlast line is empty!\n"; return 1; } } sub parenth_count { my $string = shift (@_); my $l_count = () = ($string =~ /\(/g); my $r_count = () = ($string =~ /\)/g); if ($l_count != $r_count) { print "There is no balance on this line ($l_count vs $ +r_count)! Did you format it correctly??? \n"; return 0; } else { print "There is balance, but is the logic correct?\n"; return 1; } } sub remove_all_but { my @tempArray = split( " ", shift (@_)); my $i = shift (@_); if (exists $tempArray[$i]) { my $string = $tempArray[$i]; print "\n $string \n"; return $string; } else { return " ";} } sub check_value_assigned { my $string = shift (@_); if ($string eq " "){ print "\n There is no value assigned!!!!\n"; return "undef"; } else { print "$string\n"; return $string; } } sub get_type_nohash { my $string = shift (@_); $string = remove_all_but($string, 0); print "Getting type!\n"; $string = check_value_assigned ($string); return $string; } sub get_value_type_nohash { my $string = shift(@_); $string = remove_all_but($string, 1); print "Getting value type!\n"; $string = check_value_assigned ($string); return $string; } sub get_name_nohash { my $string = shift (@_); $string = remove_all_but ($string, 2); print "Getting Name!\n"; $string = check_value_assigned ($string); return $string; } sub get_val_nohash { my $string = shift (@_); $string = remove_all_but ($string, 3); print "Getting value! \n"; $string = check_value_assigned ($string); return $string; } sub global_check { my $string = shift (@_); my $indices = index ($string, "global"); my $declarator = substr $string, $indices, 7; if ( $pre_decl ne $declarator ){ print "Global found!\n"; $pre_decl = $declarator; return 1; } else { print "No global found on this line!"; return 0; } } sub global_operations { my $string = shift (@_); my $name = get_name_nohash($string); if ((exists $hash_glob->{$name}) == 0) { print "\nMade it to the globs!\n"; $hash_glob -> { $name} = { NAME => "e\n", TYPE => "BOOLEAN", VALUE => "value" }; $hash_glob->{$name}{NAME}; } else { print "The global $name already exists!\n" ; } } sub basic_operations { my $testString = shift (@_); if (empty ($testString) == 0) { print "There is stuff in string!\n"; if (parenth_count($testString) == 1) { if (global_check($testString) == 1){ print "Global check made it through alive!\n"; global_operations($testString); } } } } my $placeholder = " "; my $baseString = "(global boolean dwood_is_leet true)"; basic_operations($baseString);


An example of a script I should be able to parse/interpret/compile:

(global short rounds_completed 0) (script static unit player (unit (list_get (players) 0))) (script static void music_ambercladremix (sound_looping_start "sound\moooseguy\sound_looping\ambercladremi +x" none 1) ) (script dormant death_cutscene (enable_hud_help_flash false) (show_hud_help_text false) (sleep 30) (camera_set death7 100) (sleep 50) (camera_set death8 110) (cinematic_set_title gameover) (sleep 150) (fade_out 0 0 0 30) (sleep 50) (cinematic_stop) (wake credits_cutscene) )

In reply to Parsing SExpressions (lisp) by Dwood

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.